.htaccess rewrite or subdomain - php

This is my url is shown at the moment;
http://blog.example.co.uk/post?id=152&title=titleofpost
This is my htaccess as shown below;
To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
Blog subdomain
RewriteCond %{HTTP_HOST} blog.example.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L]
How can I show the url as;
http://blog.example.co.uk/post/titleofpost

Have it like this:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ /post/%1/%2? [R=302,L,NE]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+author(?:\.php)?\?name=([^\s&]+)\s [NC]
RewriteRule ^ /author/%1? [R=302,L,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [L,R=302]
# internal forward from pretty URL to actual one
RewriteRule ^post/([\w-]+)/([\w-]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
# internal forward from pretty URL to actual one
RewriteRule ^author/([\w-]+)/?$ author.php?name=$1 [L,QSA,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
# Blog subdomain
RewriteCond %{HTTP_HOST} ^example\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]

Related

mod_rewrite | remove .php and force https

I created a little static website in html/js/php.
Now I only want the https:// non www. domain to be used (like this: https://zufallsgenerator.org/). All other combinations should be redirected. At the moment the site can be accessed from every possible combination...
To make the URLs more readable I also want to remove the .php extension.
I'm only able to get one of these things working. As soon as I try to combine them I get too many redirects. At the moment only the removal of the .php extension is online.
I'm using that well known snippet:
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
To redirect http to https and non www I normally use:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Any ideas how to combine them?
Is there a best practice?
I already searched a lot and only found huge .htaccess files with many many rewrite rules...
Thank you so much!
Have it this way:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

htaccess hide subdomain url parameters

This is my url is shown at the moment;
http://blog.example.co.uk/post?id=152&title=titleofpost
This is my htaccess as shown below;
To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
Blog subdomain
RewriteCond %{HTTP_HOST} blog.example.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L]
How can I show the url as;
http://blog.example.co.uk/post/titleofpost
or
http://blog.example.co.uk/titleofpost
Thanks

htaccess rule for subfolders in subdomain

I have created a subdomain to point to a folder on my root directory like this;
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
I can now access files of the /blog folder like this http://blog.example.co.uk/index.php
The /blog folder contains a /php folder containing scripts.
I'm unable to access the /php folder like this http://blog.example.co.uk/php/test.php
This doesn't work. For me to access them I have to do this http://www.example.co.uk/blog/php/test.php
Below is my current .htaccess
Options +FollowSymLinks -MultiViews
### Turn mod_rewrite on
RewriteEngine On
RewriteBase /
### Prevent rules on script folder in root /php & /blog/php because I remove .php extension from all files
RewriteRule ^(php|blog/php)($|/) - [L]
### To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
### To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
### My subdomain
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
Now I need to create a rule to point http://blog.example.co.uk/php to /blog/php
When I try to access files in the /php now I get a File Not Found error
Thanks
This is my complete htaccess. The pretty url rules are not working.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
### Blog subdomain
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]
### Careers subdomain
RewriteCond %{HTTP_HOST} ^careers\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/careers/ [NC]
RewriteRule ^(.*)$ /careers/$1 [L]
RewriteRule ^(php|blog/php|careers/php|vendors|blog/vendors)($|/) - [L]
# To remove index.php or index.html from URL
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]
RewriteCond %{REQUEST_URI} /index\.php?$ [NC]
RewriteRule ^(.*)index\.php?$ "/$1" [NC,R=301,NE,L]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^ %{REQUEST_URI}.html [L]
# external redirect from actual URL to pretty one (BLOG)
RewriteCond %{THE_REQUEST} \s/+post(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /post/%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+category(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /category/%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+author(?:\.php)?\?author=([^\s&]+) [NC]
RewriteRule ^ /author/%1? [R=302,L,NE]
# external redirect from actual URL to pretty one (CAREERS)
RewriteCond %{THE_REQUEST} \s/+role(?:\.php)?\?id=([^\s&]+)&role=([^\s&]+) [NC]
RewriteRule ^ /role/%1/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one (BLOG)
RewriteRule ^post/([\w-]+)/([\w-]+)/?$ post.php?id=$1&title=%2 [L,QSA,NC]
RewriteRule ^category/([\w-]+)/([\w-]+)/?$ category.php?id=$1&title=%2 [L,QSA,NC]
RewriteRule ^author/([\w-]+)?$ author.php?author=$1 [L,QSA,NC]
# internal forward from pretty URL to actual one (CAREERS)
RewriteRule ^role/([\w-]+)/([\w-]+)/?$ role.php?id=$1&role=%2 [L,QSA,NC]
### Error 404 redirect
ErrorDocument 404 /404page.php
Try these rules:
### Error 404 redirect
ErrorDocument 404 /404page.php
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one (BLOG)
RewriteCond %{THE_REQUEST} \s/+post(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /post/%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+category(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+) [NC]
RewriteRule ^ /category/%1/%2? [R=302,L,NE]
RewriteCond %{THE_REQUEST} \s/+author(?:\.php)?\?author=([^\s&]+) [NC]
RewriteRule ^ /author/%1? [R=302,L,NE]
# external redirect from actual URL to pretty one (CAREERS)
RewriteCond %{THE_REQUEST} \s/+role(?:\.php)?\?id=([^\s&]+)&role=([^\s&]+) [NC]
RewriteRule ^ /role/%1/%2? [R=302,L,NE]
# To remove index.php or index.html from URL
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ /$1 [NC,R=301,NE,L]
RewriteCond %{REQUEST_URI} /index\.php$ [NC]
RewriteRule ^(.*)index\.php?$ /$1 [NC,R=301,NE,L]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.html [NC]
RewriteRule ^ %1 [R=302,L]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# internal forward from pretty URL to actual one (BLOG)
RewriteRule ^post/([\w-]+)/([\w-]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
RewriteRule ^category/([\w-]+)/([\w-]+)/?$ category.php?id=$1&title=$2 [L,QSA,NC]
RewriteRule ^author/([\w-]+)?$ author.php?author=$1 [L,QSA,NC]
# internal forward from pretty URL to actual one (CAREERS)
RewriteRule ^role/([\w-]+)/([\w-]+)/?$ role.php?id=$1&role=$2 [L,QSA,NC]
### Blog subdomain
RewriteCond %{HTTP_HOST} ^blog\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ blog/$1 [L]
### Careers subdomain
RewriteCond %{HTTP_HOST} ^careers\.example\.co.uk$ [NC]
RewriteCond %{REQUEST_URI} !^/careers/ [NC]
RewriteRule ^(.*)$ careers/$1 [L]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

.htaccess rewrite rule not working?

I am using htaccess for generating clean urls in my project.
Like, I want to convert this url :
https://localhost/erp/profile.php?username=kopyo
to this url:
https://localhost/erp/profile/kopyo
My htaccess code is:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /erp
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Code I am using is this line but not working
RewriteRule profile/(.*) profile.php?username=$1
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Let me know what I am doing wrong in this. Thanks
Make sure to use proper flags after RewriteRule and re-arrange your rules:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /erp/
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,NE,L]
RewriteRule ^profile/(.+)$ profile.php?username=$1 [L,NC,QSA]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Sub-domain Redirect Using htaccess

I am working on a project that requires rewriting a URL like
https://orange.url.com
to
https://www.url.com/?s=orange
I need assistance on how to churn out htaccess to handle this task.
PS: Please not that I already have the following in my htaccess handling some rewriting for me.
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can put this code in your htaccess (which has to be in your root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
# To internally forward http://xxx.domain.com to http://www.domain.com/?s=xxx
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^$ index.php?s=%1 [L]
# To internally forward http://xxx.domain.com/yyy/ to http://www.domain.com/?s=xxx&c=yyy
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/$ index.php?s=%1&c=$1 [L]
# To internally forward http://xxx.domain.com/yyy/zzz/ to http://www.domain.com/?s=xxx&c=yyy&g=zzz
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?s=%1&c=$1&g=$2 [L]
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} \s/(.+?)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/$ $1.php [L]

Categories