I'm trying to rewrite example.com/news.php to example.com/news or example.com/news/. With my code example.com/news works just fine but example.com/news/ gives me a 404 Not Found.
My code:
<Directory /var/www/>
Options -Indexes SymLinksIfOwnerMatch -MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^games/csgo/([0-9]+)$ /games/csgo/view.php?id=$1 [L]
</IfModule>
</Directory>
How can I redirect example.com/news.php to example.com/news AND example.com/news/?
You should better stick to one url type! (with or without trailing slash) and redirect the wrong type to the other,
this code is for removing slash from the end of url :
RewriteRule ^(.*)/$ /$1 [L,R=301]
If you add RewriteCond %{REQUEST_FILENAME} !-d and RewriteCond %{REQUEST_FILENAME}.php -f before that it works also with sub directories.
Related
I have the following in my .htaccess file. Redirecting the www. works perfectly, as does the custom 404 message. The removal of .php from the file extension fails to work. domain.com/file returns the 404 message, with domain.com/file.php working fine.
RewriteEngine On
RewriteBase /
#
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
#
ErrorDocument 404 /404.php
Also if it helps this is in my 000-default.conf file
<Directory /var/www>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Write the code in .htaccess file in your project folder.it might be help for you
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
So I've been trying to rewrite my php URLs with .htaccess so that they are more SEO/user friendly. Here is my code:
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
What this does is, it turns this:
http://www.example.com/?lang=en
http://www.example.com/contact?lang=en
Into this:
http://www.example.com/en/
http://www.example.com/en/contact
The problem:
http://www.example.com/en/
is not the same thing as:
http://www.example.com/en
And I get a 404 Not Found error when I try to point my browser's address to http://www.example.com/en. How can I make both versions of this work?
Adding DirectorySlashes On to my root .htaccess did not work. But my mobile version located in the /m/ folder has the following .htaccess:
# Set dir index
DirectoryIndex index.php
# Set dir slashes
DirectorySlash On
And it works! When I visit http://www.example.com/m I get redirected to http://www.example.com/m/. That is the wanted behavior. I guess it works because /m/ is a sub directory...
The question:
How can I make http://www.example.com/en redirect to http://www.example.com/en/ without breaking the rest of my URLs?
Edit (my entire .htaccess):
# Do not remove this line or mod_rewrite rules and search engine friendly URLs will stop working
RewriteBase /
# Disable directory views
Options All -Indexes
IndexIgnore *
# Follow Symbolic links
Options +FollowSymLinks -MultiViews
# Create Directory Slashes
DirectorySlash On
# Rewrite urls
<IfModule mod_rewrite.c>
RewriteEngine on
# Pretty desktop URLs
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]*)/$ /?lang=$1 [L]
RewriteRule ^([^/]*)/prices$ /prices?lang=$1 [L]
RewriteRule ^([^/]*)/offers$ /offers?lang=$1 [L]
RewriteRule ^([^/]*)/maps$ /maps?lang=$1 [L]
RewriteRule ^([^/]*)/contact$ /contact?lang=$1 [L]
RewriteRule ^([^/]*)/links$ /links?lang=$1 [L]
RewriteRule ^([^/]*)/error$ /error?lang=$1 [L]
RewriteRule ^([^/]*)/verify-new$ /verify-new?lang=$1 [L]
RewriteRule ^([^/]*)/verify-old$ /verify-old?lang=$1 [L]
RewriteRule ^en/ski-rent-prices$ /ski-rent-prices [L]
# Rewrite mobile URLs
RewriteRule ^([^/]*)/m/$ /m/?lang=$1 [L]
# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite URL without file extensions
# For .PHP files
RewriteRule ^error$ error.php [L]
RewriteRule ^offers$ offers.php [L]
RewriteRule ^prices$ prices.php [L]
RewriteRule ^maps$ maps.php [L]
RewriteRule ^contact$ contact.php [L]
RewriteRule ^links$ links.php [L]
RewriteRule ^verify-new$ verify-new.php [L]
RewriteRule ^verify-old$ verify-old.php [L]
# For .PDF files
RewriteRule ^ski-rent-prices$ ski-rent-prices.pdf [L]
</IfModule>
# Add Custom Error pages
ErrorDocument 400 /error.php
ErrorDocument 403 /error.php
ErrorDocument 404 /error.php
ErrorDocument 500 /error.php
# Default Character set
IndexOptions Charset=UTF-8
AddDefaultCharset UTF-8
# Hide the server signature
ServerSignature Off
<IfModule mod_rewrite.c>
# Add correct content types for documents
# JS:
AddType text/javascript .js
# CSS:
AddType text/css .js
# TXT:
AddType text/plain .txt
</IfModule>
<IfModule mod_headers.c>
# IE Compability Mode
BrowserMatch (MSIE|Trident) ie
Header set X-UA-Compatible "IE=Edge,chrome=1" env=ie
# Disallow 3rd party iframes
Header always append X-Frame-Options SAMEORIGIN
</IfModule>
Rewrite --- www.example.com/?lang=en => www.example.com/en/
RewriteCond %{QUERY_STRING} (^|&)lang=en($|&)
RewriteRule ^$ /en/? [L,R]
Updated code for testing
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L,R=301]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L,R=301]
See the browser address bar and check if the redirections are correct. If it wrong provide the source link and redirected link. If it is ok, Then use the below code.
RewriteBase /
Options +FollowSymLinks -MultiViews
DirectorySlash On
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/$
RewriteRule ^(.+)/ ?lang=$1 [L]
RewriteCond %{REQUEST_URI} !^(.+)/(.+)/m/$
RewriteRule ^(.+)/(.+)/ $2?lang=$1 [L]
RewriteRule ^(.+)/(.+)/m/ $2/m/?lang=$1 [L]
I have tried the code with the below links and its working fine.
http://www.example.com/en
http://www.example.com/en/
http://www.example.com/en/contact
http://www.example.com/en/contact/
http://www.example.com/en/contact/m
http://www.example.com/en/contact/m/
http://www.example.com/en/prices
http://www.example.com/en/prices/
http://www.example.com/en/prices/m
http://www.example.com/en/prices/m/
Check these links yourself and also other links as well.
New .htaccess file (still not working, it sends me to localhost/xampp
RewriteEngine on
RewriteBase /sample/
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
Old .htaccess file below:
RewriteEngine on
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index.php
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://localhost/sample/ [R=301,L]
Domain is http://localhost/sample/. I have tried many things that Google has given me but none of them work. Every time I go to localhost/sample/index.php it doesn't redirect to localhost/sample/. I also used the Redirect 301 /index.php /.
I tried
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But it sends me to http://localhost/xampp/splash.php.
I am still new to .htaccess so I really don't have any idea what I am doing wrong. I've tried the answers that have been given to the questions where the OP said the answer worked.
You can use:
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
RewriteEngine on
RewriteBase /sample/
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
RewriteBase /sample/ is needed to make sure redirect happens from raltive path of current directory.
Use this:
RewriteEngine On
# prevent directory listing
Options -Indexes
# allows leaving off the extension (.php) in urls
Options +MultiViews
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^index(\.php)?$ http://localhost/sample [R=301,L]
i am trying to convert the actual URL to user friendly there is dynamic menu, when user click on page the orignal url becomes
http://example.com/single.php?name=mypagename
i want to change it to
http://example.com/page/mypagename
here is my htaccess file i tried from different angles Please can any one help to correct it..
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymlinks -Multiviews
Options +SymLinksIfOwnerMatch -Multiviews
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /single\.php(\?|\ )
RewriteCond %{QUERY_STRING} name=(.+)
RewriteRule page/(.*) single.php?name=$1
RewriteRule ^ /page/%1? [L,R=301]
You must rearrange your directives:
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymlinks -Multiviews
Options +SymLinksIfOwnerMatch -Multiviews
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+single\.php\?name=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [L,R=301]
RewriteRule ^page/(.*)$ single.php?name=$1 [L,NC,QSA]
RewriteCond %{THE_REQUEST} \s/+(Web/2015/wessexcars)/internalpage\.php?seo=\?name=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [L,R=301]
RewriteRule ^(Web/2015/wessexcars)/(.+?)/?$ $1/internalpage.php?seo=$1 [L,NC,QSA]
Try like bellow,
RewriteEngine on
RewriteRule ^/page/([a-zA-Z-0-9-]+)$ $1/single.php?name=$2 [NC]
firstly change your config file make index file blank
$config['index_page'] = '';
and make a htaccess file
<ifmodule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</ifmodule>
An obsolete Wordpress image plugin has left me with over 4000 404 errors.
To solve the problem, I'd like to batch 301 redirect thousands of old attachment link URLs to point towards the original source image files:
The old URLs that give a 404 error look like this:
http://www.hongkonghustle.com/?pagename=album&?pp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg
I'd like to redirect them each to the wp-content/photos directory where the file actually exists:
http://www.hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg
From what I've read, I believe I should alter my htaccess file and add a RewriteCond using the {QUERY_STRING} but I don't know exactly how.
Also, where should I put the changes relative to my current htaccess?
My current htaccess file includes the following:
Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]
RewriteEngine On
RewriteBase /
<Files wp-config.php>
Deny from all
</Files>
<Files wp-config.php>
Deny from all
</Files>
Options +Indexes
IndexOptions -FancyIndexing
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Thanks again Anubhava! You're getting very close!
Now it goes to the /wp-content/photos/ directory, but the URL is still incorrect:
hongkonghustle.com/wp-content/photos/&/?pagename=album&%253fpp_album=main&pp_cat=default&pp_image=zombie_boy_tattoo_lady_gaga_rick_genest.jpg
We need the final URL to be:
hongkonghustle.com/wp-content/photos/zombie_boy_tattoo_lady_gaga_rick_genest.jpg
So, hongkonghustle.com/wp-content/photos/IMAGE_FILENAME.JPG
I think it's almost solved! Please update me if you have any ideas on how to accomplish this. Sincere thanks!
I ended up doing a 301 redirect like this in htaccess:
Options +Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^pagename=album&\??pp_album=main&pp_cat=default&pp_image=(.*)$
RewriteRule .* /wp-content/photos/%1? [L,R=301]
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1 [R=permanent,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://www.hongkonghustle.com/$1/ [L,R=301]
RewriteEngine On
RewriteBase /
<Files wp-config.php>
Deny from all
</Files>
<Files wp-config.php>
Deny from all
</Files>
Options +Indexes
IndexOptions -FancyIndexing
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Change your code to this:
Options +Indexes +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)pp_image=([^&]+) [NC]
RewriteRule ^$ /wp-content/photos/%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^hongkonghustle\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
<Files wp-config.php>
Deny from all
</Files>
IndexOptions -FancyIndexing