Jump to content

Recommended Posts

I need help with a .htaccess modRewrite command.  I'm trying to convert this url:

 

http://claninfectionist.com/tutorials/?cat=photoshop&tut=pipe

 

To this:

 

http://claninfectionist.com/tutorials/photoshop/pipe

 

To help with SEO.  The thing is...well, a couple things, the tutorials section was set up after my site and I already had a few up, I was using a file based system before I went to database so my images in my tutorials are ../imagename.png, not actual filepaths.  I use the cat and tut values to replace all ../'s with [category]/[tutorial]/imagename.png, so that needs to be preserved, it would be too much work to fix all the tutorials.

 

This is the RewriteRule I tried, it didn't seem to work:

 

rewriterule ^([^-]+)/([^&]+)$ index\.php?cat=$1&tut=$2

 

I also need it to rewrite when the user is viewing just the category:

 

http://claninfectionist.com/tutorials/?cat=photoshop

 

to

 

http://claninfectionist.com/tutorials/photoshop

 

Or if index.php is included in the url:

 

http://claninfectionist.com/tutorials/index.php?cat=photoshop&tut=pipe

 

to

 

http://claninfectionist.com/tutorials/photoshop/pipe

 

And still maintain that system of replacing the ../'s.

 

Any help would be very nice.  TYIA!

Link to comment
https://forums.phpfreaks.com/topic/84275-modrewrite-help/
Share on other sites

Not sure if you have found a solution yet. Looks like you may after checking your site.  ;)

 

Your requirements are a little bit more flexible than first appears.

I managed to simulate this on my local machine using localhost.com but not using .htaccess files.

If you use .htaccess files remember to add a RewriteBase statement

The "empty case" for the index file (/tutorials/?) complicates rewrites since an extra otherwise / has to be added

Also, since the rewritten local file isn't "passed thru" to the mod alias etc. the user "sees" the original URL (/tutorial) even though the correct file is retrieved. Also, because of this you need to always "force" the correct index filename (index.php coded as default).

Generally I'd have thought this would be better done using a redirection (code 30x) - particularly since you're using PHP - PHP might have been a better bet for generating redirection.

 

Try this...

 

RewriteEngine On
RewriteCond %{QUERY_STRING} ^cat=(.*)&tut=(.*)$ [NC]
RewriteRule ^/tutorials/$ /tutorials/%1/%2/index.php? [NC,L]
RewriteCond %{QUERY_STRING} ^cat=(.*)&tut=(.*)$ [NC]
RewriteRule ^/tutorials/(.*) /tutorials/%1/%2/$1? [NC,L]

RewriteCond %{QUERY_STRING} ^cat=(.*)$ [NC]
RewriteRule ^/tutorials/$ /tutorials/%1/index.php? [NC,L]
RewriteCond %{QUERY_STRING} ^cat=(.*)$ [NC]
RewriteRule ^/tutorials/(.*) /tutorials/%1/$1? [NC,L]

 

Link to comment
https://forums.phpfreaks.com/topic/84275-modrewrite-help/#findComment-433936
Share on other sites

I tried your code and it didn't work.  If I go to:

 

http://claninfectionist.com/tutorials/?cat=photoshop&tut=pipe

 

The page shows up just fine, but when I go to:

 

http://claninfectionist.com/tutorials/photoshop/pipe

 

Like I want it to be displayed, it gives me my 403 access forbidden error page.  I basically just want to get rid of the dynamic url's and go with the standard folder heirarchy.  I know it can be done with htaccess as I've done it with other things, but those only had one url variable, this has the potential for 2.

Link to comment
https://forums.phpfreaks.com/topic/84275-modrewrite-help/#findComment-437602
Share on other sites

Check your logs to see what actual URL is being resolved and triggering the 403 error (check both error and access logs)

 

It might help to post the line from each log (error/access) re: this 403 error

 

Rule out access rights issues at this stage before moving back to look at the rewrite rule (because it's quick and easy to do it at this stage ;) )

 

If the path being denied is not what you expected go back to the rule and enable rewrite logging to check if the rule is resolving to the correct physical path.

 

If the path really is being rewritten to what you expected then check access rights and content

Link to comment
https://forums.phpfreaks.com/topic/84275-modrewrite-help/#findComment-437936
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.