Redirect request and hide .php extension from the request - apache

We have started to change our website, and we use the old and the new site together. If something not exist on the new site we just redirect them back to the old one. (This is a transparent process for the user.)
This is how .htaccess rules looks like now:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain.hu [NC]
RewriteCond %{http_host} !^domain.hu.data18.websupport.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.*)$ /static-old/$1 [QSA]
RewriteRule ^(.*)\.html$ $1.php [L,R=301,QSA]
So if we don't find the the file on the main directory, but we find it in the old one we serve the old one instead of a 404. And if the requested file has html extension then we rewrite it internally to php, because we changed this a while ago.
I want to hide the php extension (and so html will be hidden too) so if a requested uri is /something.php or /something.html then only show /something to the user. But I if I edit .htacces like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} ^(.*?)\.php[\s?] [NC]
RewriteRule ^/%1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old-from-seocms%{REQUEST_URI} -d
%{REQUEST_URI} !\.(.*)$
RewriteRule ^(.*)$ $1.php [L,R=301,QSA]
RewriteRule ^(.*)$ /static-old-from-seocms/$1 [QSA,L]
I will get an 500 internal server error, the .htaccess parsing fail.
I think this script will check if the requested file has php extension and does not exist in the main directory then redirect it to the same address without the php extension, after that the script will check if the requested uri is not exist in the main directory and does not have an extension then write it a php extension to it internally.

Try this .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{http_host} ^domain\.hu [NC]
RewriteCond %{http_host} !^domain\.hu\.data18\.websupport\.sk$ [NC]
RewriteRule ^(.*)$ http://www.domain.hu/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.(php|html)[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static-old%{REQUEST_URI} -d
RewriteRule ^(.+)$ /static-old/$1 [L]

Related

pretty url with folder name and two variables

ugly url - example.com/folder_name/art.php?id=9&s=something
want to be - example.com/folder_name/id/s
here is my try
# external
RewriteCond %{THE_REQUEST} \s/+art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ art.php?id=$1&s=$2 [L]
doesn't work
entering ugly url - address bar is unchanged
entering pretty url - getting error 404
pls help
You rules will have to allow folder_name as well so use:
RewriteEngine On
# external
RewriteCond %{THE_REQUEST} \s/+(folder_name)/art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(folder_name)/([\w-]+)/([\w-]+)/?$ $1/art.php?id=$2&s=$3 [L,QSA,NC]
This assumes there is no other .htaccess in folder_name/.
In case there is a .htaccess inside that subfolder then use this .htaccess:
RewriteEngine On
RewriteBase /folder_name/
# external
RewriteCond %{THE_REQUEST} /art\.php\?id=([^&]*)&s=([^\s&]*) [NC]
RewriteRule ^ %1/%2? [R=301,L]
# internal
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ art.php?id=$1&s=$1 [L,QSA]

htaccess rewrite rule all existing files and directories in one domain go to same filepath in another domain

I'm trying to write an htaccess rewrite rule to send all existing files and directories at the domain http:// or https:// and www or not lun.re to lunre.com.
So lun.re/admin, which does exist would go to lunre.com, but lun.re/new, which doesn't exist would stay at lun.re, but would redirect to https://lun.re/new.
Here's what I have so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{HTTP_HOST} ^lun\.re$ [OR]
RewriteCond %{HTTP_HOST} ^www\.lun\.re$
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^(.*)$ https://lunre.com/ [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Lines 5-8 were already there from my hosting provider.
I've also tried this:
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /loader.php [L,R]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]
But then I get a too many redirects error.
What exactly I am trying to do is if it does exist and the domain is lun.re then go to https://lunre.com/file-or-dir (the same filepath at the long url) otherwise if it doesn't exist then stay at the current domain and go to https://currentdomain/loader.php
You can use this:
RewriteEngine on
# http to https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
#redirect all existing files/directories to http://lunre.com
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{HTTP_HOST} ^(www\.)?lun.re$
RewriteRule ^ http://lunre.com/ [L,R]
You don't need a RewriteRule to Redirect non-existent requests back to your main domain as the Rule only redirects existing files/folders.

Apache .htaccess rewrite all but one

I have an Angular2 application, to get the routes working properly I've setup a rewrite in my .htaccess to point at /
Now my problem is that I need to redirect an API I wrote, which is not working with my current rewrite rules.
Here is my .htaccess
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^/api$ http://127.0.0.1:8888/ [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/api/(.*)$ http://127.0.0.1:8888/$1 [P,L]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]
The first rewrite should let me hit my API at
HOSTNAME/v1/api/etc/etc/etc/someParam
and the second should let my Angular2 routes work.
Both of these rules are hitting my Angular2 application.
How do I make two rules where the first says /api/**** rewrites to localhost:8888/api/v1/....
And then everything else gets directed at my Angular2 application?
You want to split this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/api/(.*)$ http://127.0.0.1:8888/$1 [P,L]
Into two rules.
Solution:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/api/****$ http://localhost:8888/api/v1/... [P,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
ReweiteCond %{REQUEST_URI} !^/api/****$ [NC]
RewriteRule ^/api/(.*)$ http://127.0.0.1:8888/$1 [P,L]
Of course, you've gotta replace **** and ... accordingly.

Dynamic htaccess code for multiple php parameters to friendly urls

I have searched and searched for an answer to this, but none of the posts I've found on stackoverflow work for me - nor do the online htaccess generators.
I have made slight adjustments to Anubhava's excellent answer on htaccess redirect for dynamic urls not working to suit different page names as follows:
RewriteEngine On
# for external redirection from `/hp.php?su=sitename` to `/sitename`
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
# for internal redirection from `/sitename` to `/hp.php?su=sitename`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
However, in addition to sending users to domain.com/sitename (which the modified code above does) I also want to change the following urls.
domain.com/newpage.php?su=sitename&PgID=1234&pu=pagename
to become
domain.com/sitename/1234/pagename.html
similary
domain.com/diary.php?su=sitename
to become
domain.com/sitename/diary.html
This last one would be replicated for similar dynamic pages, such as
future.php?su=sitename >> domain.com/sitename/future.html
photos.php?su=sitename >> domain.com/sitename/photos.html
etc
Some time in the future, I would also like to divert http:// to https:// - would this rule go before all the others?
Hope somebody can help
UPDATE:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&PgID=$2&pu=$3 [L,QSA]
It isn't all that difficult to derive from the previous rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/hp\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/newpage\.php\?su=([^\s&]+)&PgID=(\d+)&pu=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3.html? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/(diary|future|photos)\.php\?su=([^\s&]+) [NC]
RewriteRule ^ /%2/%1.html? [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /hp.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(diary|future|photos).html$ /$2.php?su=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/(\d+)/(.+)\.html$ /newpage.php?su=$1&pgID=$2&pu=$3 [L,QSA]
As to your second question about http to https, yes; that particular redirection should occur before all others.

redirect with .htaccess rewrite rule to different folder on my server

I have been trying to redirect to a different folder with .htaccess when I hit a domain.
Here's my .htaccess redirect rule, can you tell me where am I wrong?
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
So, what I want is, if someone visits racereadymotorsports.in, they redirect to /raceready/ directory
and on the other hand if they visit to annelies-slabbynck.com, they should redirect to /annelies/ directory
At present, this redirects twice for annelies-slabbynck.com which means that, I end up with:
http://www.annelies-slabbynck.com/annelies/annelies/ as my final url
I am running a shared hosting and do not have access to add a new configuration.
Please help.
You need to remove the [OR] flag and reproduce the same conditions with the other rule. Maybe something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Or better yet, you can use these conditions instead: RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?racereadymotorsports.in$
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/raceready/$1 -d
RewriteRule ^(.*)$ /raceready/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(www.)?annelies-slabbynck.com$
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/annelies/$1 -d
RewriteRule ^(.*)$ /annelies/$1 [L,R=301]
Note that the R=301 flag in your second rule redirects the browser, unlike the first rule which only internally rewrites the request.