3

I have a site with pages like about.php, contact.php and test.php?id=xyz.

I added the below redirect for making it seo friendly.

Options -Indexes -MultiViews RewriteEngine on RewriteCond %{HTTP_HOST} !^www\.krishibazar\.org$ [NC] RewriteRule ^(.*)$ http://www.krishibazar.org/$1 [R=301,L] # To externally redirect /dir/file.php to /dir/file RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC] RewriteRule ^ /%1 [R=301,L,NE] # To internally forward /dir/file to /dir/file.php RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC] RewriteRule ^(.+?)/?$ /$1.php [L] 

This gives me

mydomain.com => www.mydomain.com mydomain.com/about.php => www.mydomain.com/about mydomain.com/test.php?id=12 => www.mydomain.com/test?id=12 mydomain.com/about/ => CSS Error 

It is working perfectly fine except if i add a trailing slash at the last its giving error. So either suggest me to prevent the error or adding a trailing slash.

2
  • add this just below <head> section of your page's HTML: <base href="/" /> Commented Mar 16, 2016 at 20:07
  • This worked for me as well except now about and about/ both are opening. It might create SEO duplicates. What do you say ? Commented Mar 16, 2016 at 20:31

1 Answer 1

1

Option 1: Get rid of the slashes (good for SEO)

How about an additional rule to just handle URL with trailing slashes?

I would place it between the externally and internally redirects above, like this:

RewriteRule ^(.*)/$ /$1 [L,R=301] 

That should redirect all URL to their slash-free version before they are rewritten to the PHP file.

Option 2: Fix your CSS

I suppose you are referencing your stylesheet relatively, like

<style href="css/style.css" /> 

You might change it to an absolute reference

<style href="/css/style.css" /> 

And it should work regardless of the assumed directory depth (denoted by slashes in the URL).

Conclusion

I would go for option 1 in this case, as it provides a single URL for all pieces of content, which is preferrable in my opinion.

Sign up to request clarification or add additional context in comments.

6 Comments

First one worked flawlessly for me. Thanks @Jan for your conclusion which helped me in deciding the best.
I noticed s a serious problem after adding the above htaccess code. All my Ajax scripts in the pages stopped working. After removing the htaccesss file its working. Can it be solved ?
What do those AJAX URL look like?
$.ajax({ url:"verify.php", //the page containing php script type: "POST", //request type data: { type: "send", to: "<?php echo $user;?>"}, success:function(result){ alert(result);
Is the call redirected to /verify, being caught be the rule # To externally redirect /dir/file.php to /dir/file?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.