.htaccess: redirect specific link to another?

.htaccess: redirect specific link to another?



I have these three links:


localhost/my_projects/my_website.php
localhost/my_projects/my_website.html
localhost/my_projects/my_website



The paths of the php and html files are as follows:


C:xampphtdocsmy_projectsmy_website.php
C:xampphtdocsmy_projectsmy_website.html



The link without an extension is "artificial" and I want to use said link:


localhost/my_projects/my_website



to get the contents of either of these links:


localhost/my_projects/my_website.php
localhost/my_projects/my_website.html



The reason for the two example files, instead of just one, is that I want to be able to switch between those two files when I edit the htaccess file. Obviously I only want to access one of those files at a time.



What do I need to have in my .htaccess file inside the my_projects folder to accomplish that? How can I make one specific link redirect to another specific link?






Can you add the current .htaccess to the question?

– Juan
Sep 11 '18 at 23:17






@Juan It's basically empty. I tried many things, but none of them worked. The folder im trying to manipulate (my own folder, my_projects) didn't have an .htaccess file before.

– noClue
Sep 11 '18 at 23:19






which is the document root?

– Juan
Sep 11 '18 at 23:27






Well, it's localhost. I have my xampp saved in C:/, so I assume the document root is C:xampphtdocs, where my "my_projects" folder is... sorry if I'm being stupid, does that info help?

– noClue
Sep 11 '18 at 23:30





1 Answer
1



After reading your comment clarifying your folder structure I corrected the RewriteRule. (By the way, it would be best if you add that info to the question itself instead of in comments).



The url you want to target is: http://localhost/my_projects/my_website


http://localhost/my_projects/my_website



http:// is the protocol
localhost is your domain (it could also be 127.0.0.1 or a domian name like www.example.com in the Internet)


http://


localhost


www.example.com



I assume you are running Apache on port 80, otherwise in the url you need to also specify the port. For port 8086 for example it would be http://localhost:8086/my_projects/my_website.


80


8086


http://localhost:8086/my_projects/my_website



The real path is htdocs/my_projects/my_website.php or htdocs/my_projects/my_website.html depending on your needs (obviously both won't work at the same time).


htdocs/my_projects/my_website.php


htdocs/my_projects/my_website.html



Here the my_projects in the "fake" url collides with the real folder "my_projects" so Apache will go for the folder and see there is no my_website (with no extension) document there (it won't reach the rewrite rules).


my_projects


"my_projects"



There is a question in SO that provides a work around for this, but it is not a perfect solution, it has edge cases where the url will still fail or make other urls fail. I had posted it yesterday, but I seem not to find it now.



The simple solution if you have the flexibility for doing it is to change the "fake" url for it not to collide with the real path.



One option is for example to replace the underscores with hyphens.
Then you would access the page as http://localhost/my-projects/my-website if you want to keep a sort of "fake" folder structure in the url. Otherwise you could simply use http://localhost/my-website.


http://localhost/my-projects/my-website


http://localhost/my-website



Here are both alternatives:


# This is for the directory not to be shown. You can remove it if you don't mind that happening.
Options -Indexes

RewriteEngine On

#Rule for http://localhost/my-projects/my-website
RewriteRule ^my-projects/my-website(.+)?$ my_projects/my_website.php$1 [NC,L]

#Rule for http://localhost/my-website
RewriteRule ^my-website(.+)?$ my_projects/my_website.php$1 [NC,L]



(Don't use both, just choose one of these two, or use them to adapt it to your needs)



The first part the rewrite rule is the regular expression for your "fake" url, the second part is the relative path of your real folder structure upto the page you want to show.



In the regular expression we capture whatever what we assume to be possible query parameters after .../my_website, and paste it after my_website.php in the second part of the rule (the $1).


.../my_website


my_website.php


$1



Later on if you want to point the url to my_website.html, you have to change the second part of the rule, where it says .php, replace it by .html.


my_website.html


.php


.html



By the way, it is perfectly valid and you'll see it in most SEO friendly web sites to write an url as http://www.somesite.com/some-page-locator, and have a rewrite rule that translates that url to a page on the website, which is what I had written in my first answer.


SEO


http://www.somesite.com/some-page-locator






Let us continue this discussion in chat.

– noClue
Sep 13 '18 at 12:29






My reaction when I finally figured it out. It was supposed to be: RewriteEngine On RewriteRule ^my_website/?$ my_website.php [NC,L]

– noClue
Sep 14 '18 at 3:15



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế