I have the following URL:
http://example.org/search/search
Using htaccess, I need to remove the first /search from the URL so that the URL is:
http://example.org/search
For the life of me I can't figure out how.
Here's the relevant section of my htaccess file
# Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
# Remove the need for the php file extension
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.org/$1 [R=302,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
# Remove index from home URL
RewriteRule ^(.*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
Thanks
Not sure how the rules that you have ever worked. You have some duplicate rules and redirects that interfere with routing. Put all your redirects in the same place, and make sure you use the -f check before you add the php extension:
# Remove www from URL
RewriteCond %{HTTP_HOST} ^www\.example\.org$ [NC]
RewriteRule ^(.*)$ http://example.org/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} \ /+(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.org/$1 [R=302,L]
# Remove index from home URL
RewriteCond %{THE_REQUEST} \ /+(.*)index\.php
RewriteRule ^(.*)index\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
# Remove the search folder:
RewriteCond %{THE_REQUEST} \ /+search/search
RewriteRule ^search/search(.*)$ /search$1 [L,R=301]
# Add the search folder back
RewriteCond $1 !^/search
RewriteRule ^search(.*)$ /search/search$1 [L]
# Add the extension back on if it exists
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
Related
I've been trying to achieve the following rewrite/redirect on a website I'm building but I'm struggling and can't find the right answer online.
I want to rewrite
http://www.example.com/php/someFile.php
To:
example.com/someFile
I have managed to achieve most of the desired result with the following code in my .htaccess root file but I need help with removing the php subdirectory:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
Any help would be hugely appreciated.
Have it this way in your site root .htaccess:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
# internally map /file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]
Make sure to test after clearing your browser cache.
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301,NE]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/php/file\.php\s [NC]
RewriteRule ^ file [R=301]
RewriteRule ^ php/file.php [L]
I am working according to this question
(Redirect .php urls to urls without extension)
but this sends me to root folder.
RewriteEngine On
# browser requests PHP
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.php
RewriteRule ^/?(.*)\.php$ /$1 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/?(.*)$ /$1.php [L]
for eg. my orginal url http://www.website.com/products/abc.php
after redirect it sends me to http://www.website.com/abc
Here is a generic rule that can handle subfolders
Options -MultiViews
RewriteEngine On
# browser requests PHP
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.+/)?([^/\s\?&]+)\.php
RewriteRule ^ /%1%2 [L,R=301]
# check to see if the request is for a PHP file:
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ /$1.php [L]
I'm trying to change http://www.mywebsite.com/en/admin/index?page=archived
to
http://www.mywebsite.com/en/admin/page/archived/
I've tried a bunch of things including:
RewriteCond %{QUERY_STRING} ^page=(.+)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/en/admin/%1? [R=301,L]
but i have always a 404 error.
My .htaccess is into admin directory
.htaccess :
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://%{HTTP_HOST}/en/admin/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/en/admin/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
RewriteCond %{THE_REQUEST} /index\?page=(.+?) [NC]
RewriteRule ^index$ /en/admin/page/%1? [NC,L,R]
RewriteRule ^page/([^/.]+)/?$ /en/admin/index?page=$1 [NC,L]
You can use the following code in admin/.htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\?page=([^\s]+) [NC]
RewriteRule ^ /en/admin/page/%1? [NC,L,R]
RewriteRule ^page/([^/.]+)/?$ /en/admin/index?page=$1 [NC,L]
I'm hiding my file extensions with
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
When I navigate to this link
http://localhost/website/profile?user=user01&nofollow=1
I'm redirected here
http://example.com/folder/profile?user=user01
I've taken .php out of the url. Why does it redirect me?
Have it this way inside /website/.htaccess:
RewriteEngine On
RewriteBase /website/
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php[?\s] [NC]
RewriteRule ^(.+)\.php$ $1 [R=301,L,NC]
# Resolve .php file for extension-less php urls
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/.]+)$ $1.php [L]
Make sure to clear your browser cache before testing this.
Try using this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,NE]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
This is my code:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
But it only work for the site example.com
How can I edit it to work for any site ?
If you want it to work for multiple hosts just use the variable HTTP_HOST instead of the actual site name.
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://%{HTTP_HOST}/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]