How to 301 Redirect /page.php to /page, after a RewriteRule has already made /page.php accessible by /page (Getting errors)
-
A site has its URLs with php extensions, like this: example.com/page.php
I used the following rewrite to remove the extension so that the page can now be accessed from example.com/page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]It works great. I can access it via the example.com/page URL.
However, the problem is the page can still be accessed from example.com/page.php. Because I have external links going to the page, I want to 301 redirect example.com/page.php to example.com/page. I've tried this a couple of ways but I get redirect loops or 500 internal server errors.
Is there a way to have both? Remove the extension and 301 the .php to no extension?
By the way, if it matters, page.php is an actual file in the root directory (not created through another rewrite or URI routing).
I'm hoping I can do this, and not just throw a example.com/page canonical tag on the page.
Thanks!
-
Hi,
You might want to try this solution:
Redirects domain.com/file.php to domain.com/file
RewriteCond %{REQUEST_FILENAME} !-d # is not directory
RewriteCond %{REQUEST_FILENAME}.php -f # is an existing php file
RewriteCond %{REQUEST_URI} ^(.+).php$ # request URI ends with .php
RewriteRule (.*).php$ /$1 [R=301,L] # redirect from file.php to fileSource: http://stackoverflow.com/questions/1992183/how-to-hide-the-html-extension-with-apache-mod-rewrite (2nd answer)
Dirk
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
Redirect root to detailed page
Hello, Is it a good idea to forward the root of my website (https://exmaple.nl) to example.nl/keyword (And will it be helpful)?
Intermediate & Advanced SEO | | AnthonMedia
I;m trying to get a higher ranking on a specifiek keyword. And are the possibilities in the future to reuse the homepage without losing ranking for example.nl/keyword (if my idea works)?1 -
How to redirect an url in .htaccess when "redirect 301" doesnt work
I have an odd page url, generated by a link from an external website, it has: %5Cu0026size=27.4KB%5Cu0026p=dell%20printers%20uk%5Cu0026oid=333302b6be58eaa914fbc7de45b23926%5Cu0026ni=21%5Cu0026no=24%5Cu0026tab=organic%5Cu0026sigi=11p3eqh65%5Cu0026tt=Dell%205210n%20A4%20Mono%20Laser%20Printer%20from%20Printer%20Experts%5Cu0026u=fb ,after a .jpg image url, and I can't get it redirect using the redirect 301 in .htaccess to the properly image url as I use to do with the rest of not found urls eg: /15985.jpg%5Cu0026size=27.4KB%5Cu0026p=dell%20printers%20uk%5Cu0026oid=333302b6be58eaa914fbc7de45b23926%5Cu0026ni=21%5Cu0026no=24%5Cu0026tab=organic%5Cu0026sigi=11p3eqh65%5Cu0026tt=Dell%205210n%20A4%20Mono%20Laser%20Printer%20from%20Printer%20Experts%5Cu0026u=fb to just: /15985.jpg
Intermediate & Advanced SEO | | Status0 -
Google Webmaster Tools > HTML Improvements > 301 Moved Permanently pages - how did they even get there?
Hello experts! I'm going through my Google Webmaster Tools > HTML Improvements looking for pages with duplicate meta descriptions/titles that I can fix. And I noticed there are about 60 pages odd looking page titles that have duplicate meta descriptions, which are also noted as: 301 Moved Permanently Moved Permanently The document has moved here. Apache Server at sports When I click on the link to see the page names, all of them are pages we never created. The pages are all sports blog related. Here are few examples: http://www.titanium-jewelry.com/justin-tuck-blog.html http://www.titanium-jewelry.com/unlimited-potential-project-blog.html http://www.titanium-jewelry.com/left-handed-baseball-glove-blog.html http://www.titanium-jewelry.com/adjustable-basketball-hoops-blog.html how did they get on our site? Is this some sort of malicious attack? Most of them are sports related blog looking names. I just don't know how these pages could have been created. 2) is this hurting us with Google?3) Can you tell when the page was created?Thanks ron xEtX3op.jpg
Intermediate & Advanced SEO | | yatesandcojewelers0 -
Should /node/ URLs be 301 redirect to Clean URLs
Hi All! We are in the process of migrating to Drupal and I know that I want to block any instance of /node/ URLs with my robots.txt file to prevent search engines from indexing them. My question is, should we set 301 redirects on the /node/ versions of the URLs to redirect to their corresponding "clean" URL, or should the robots.txt blocking and canonical link element be enough? My gut tells me to ask for the 301 redirects, but I just want to hear additional opinions. Thank you! MS
Intermediate & Advanced SEO | | MargaritaS0 -
Does 301 Redirect works on social signals?
Hello, I'm considering strategic change in my site's formation.
Intermediate & Advanced SEO | | BeytzNet
301 redirecting some of the pages is not a problem but the question remains - what will happen with all of my social signals? These pages have Likes, plus ones, and tweets. Thanks0 -
New to SEO. How do I set up a 301 Redirect? What Else should I do?
Hello, I am new to web design. I designed my own site using dreamweaver and did all my seo on my own, I read a few books. Long story short I rank on the bottom of page 1 just after 3 months and the keywords are highly competitive. Now, I am up against some heavy hitters from national brands versus my local real estate site. I don't have a 301 redirect, and am not sure what else I should be doing to get my site ranked higher. I have back links from various sites, ( non-paid ) so it's what others call white hat. When I grade my site on website grader I get a great score versus the sites that are higher than me. I'm guessing my sites age is an issue. I guess I'm looking for some guidance. Thank you all, Here is my site to view. http://www.bronxpad.com
Intermediate & Advanced SEO | | bronxpad0 -
Move to a new domain - How to get the redirects right?
Hi, I hope that someone with good htaccess knowledge can help. I am planning on moving my site to a new domain. I understand that I should make 301 redirects using htaccess for every URL. This is a big site so it will be impossible to not forget some of the thousands existing URLs. Now to the question: How do I make all the URLs that is not specified in htaccess to redirect to my new root? Thanks!
Intermediate & Advanced SEO | | mrlolalot0 -
Generating 404 Errors but the Pages Exist
Hey I have recently come across an issue with several of a sites urls being seen as a 404 by bots such as Xenu, SEOMoz, Google Web Tools etc. The funny thing is, the pages exist and display fine. This happens on many of the pages which use the Modx CMS, but the index is fine. The wordpress blog in /blog/ all works fine. The only thing I can think of is that I have a conflict in the htaccess, but troubleshooting this is difficult, any tool I have found online seem useless. Have tried to rollback to previous versions but still does not work. Anyone had any experience of similar issues? Many thanks K.
Intermediate & Advanced SEO | | Found0