.htaccess rewrite rule giving server error - .htaccess

I think I just need a second pair of eyes for this as I can't see why i'm getting a server error.
RewriteEngine On
RewriteRule ^Gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^Gig/([a-zA-Z0-9_-]+)/$ gig.php
#allow non caps
RewriteRule ^gig/([a-zA-Z0-9_-]+)$ gig.php
RewriteRule ^gig/([a-zA-Z0-9_-]+)/$ gig.php
Edit:
I have now viewed the log and the reason is there are far too many internal redirects. I myself am not too competent at mod_rewrites etc so please have a look.
#redirect so home page shows /Home
Redirect /home.php http://localhost/Home
#add php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Redirect /home.php http://localhost/Home
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteRule ^(.*)\.php /$1 [R=301,L]
#redirect www to non-www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.^([a-zA-Z0-9_-]+)$ [NC]
RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]
#remove trailing slash
RewriteEngine on
RewriteRule ^(.*)/$ /$1 [L,R=301]
#allow artistprofile nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/?$ artist_profile.php
#info nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Aa]bout/?$ artist_about.php
#gigs nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Gg]igs/?$ artist_gigs.php
#tracks nice url
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_\-]+)/[Tt]racks/?$ artist_tracks.php
#gig nice url
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
That's all the rewrites in the .htaccess file
Edit:
The problem is the adding PHP extension part
#add php extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I think its due to the ([a-zA-Z0-9_-]+) being after the file name.

You need to escape your - characters when they're inside brackets. You can also shorten your code to the following:
RewriteEngine On
RewriteRule ^[Gg]ig/([a-zA-Z0-9_\-]+)/?$ gig.php
# Escape it! ^^
Why can we shorten it like this?
[Gg] means "The character G or g"
/? means "/ repeated 0 or 1 time"

Related

Url rewriting rule with .htaccess file

I'm here to ask for advice about the .htaccess file that I can't configure as I would like.
I have a site, let's say :
www.mysite.com/php/blog.php
I would like the visitor to see in the address bar only:
www.mysite.com/blog
This is what the .htaccess file contains:
Options +FollowSymlinks
Options All -Indexes
RewriteEngine On
RewriteRule ^$ index.php #This line is to hide index.php on the home page, maybe it's not the right way to do it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^php/([0-9a-zA-Z]+).php$ $1 [L] #I tried to write this, but it doesn't work
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I suspect that this question has been asked many times, but I have really tried, without success.
Can you help me, please?
Thanks !
Your patter is wrongly expecting URI to start with /php/ which is not the case here. Also keep http -> https rules at top and then handle .php extension rule like this:
RewriteEngine On
# http -> https redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# To externally redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?(\S+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]

How do I reduce redirect chain with .htaccess?

I've been having some problem with solving the redirect chain of my website. I wanted my website to ultimately have only one version, which is HTTPS + non-www + trailing slash
When tested, below htaccess gave me 4 response with redirect chain out of 8 total variation tested (https, www/non-www,trailing slash, 2x2x2 = 8 variations).
The http://www. variation gave me a redirect chain with 3 redirects
http://www. --> https://www. --> https://www. + trailing slash ---> https:// + trailing slash
Is there a way that I can make it no redirect chain or at least not more than 2?
Appreciate if someone could help me out! Thanks
# Force trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
# HTTPS forced by SG-Optimizer
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
</IfModule>
# END HTTPS
# 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
I use this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [L,R=301]
</IfModule>
If I understood you correctly, you want to force HTTPS//non-WWW and remove trailing slashes from files in one redirect. That's a bit demanding and require some sort of GOTO/SKIP logic in the .htaccess file. Try it like this with a fresh browser in incognito mode:
RewriteEngine on
## Check if not a directory and ends in /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
## If not a directory skip next RewriteRule
RewriteRule ^ - [S=2]
## Check if HTTPS and non-WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
## This RewriteRule skipped if URI was a directory
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
## This RewriteRule used if URI was a directory
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [S=1]
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]
PS: I get the idea, but Google's Gary Illyes tweeted in 2016 "30x redirects don't lose PageRank anymore." So, even for SEO this is no longer a burning issue.

Show url of index page with .htaccess

I have googled around and can't find an answer to my question so her I am. How can I force, with .htaccess, the filename of the index page to show when accessed by just the domain.
So if http://example.com is typed in http://examples.com/home will show in the address bar.
I have already used .htaccess to set home.php as the index page and remove the .php extension.
current .htaccess
DirectoryIndex home.php
RewriteEngine on
#add php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{REQUEST_URI} !^/(phpscripts|js)/ [NC]
RewriteRule ^([^/]*)(?:/(.*?|))?/?$ /$1.php?$2 [L]
# redirect to .php-less link if requested directly
RewriteCond %{THE_REQUEST} ^[A-Z]+\s.+\.php\sHTTP/.+
RewriteCond %{REQUEST_URI} !^/(phpscripts|js)/ [NC]
RewriteRule ^(.*)\.php /$1 [R=301,L]
#redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.^([a-zA-Z0-9_-]+)$ [NC]
RewriteRule ^(.*)$ http://^([a-zA-Z0-9_-]+)$1 [L,R=301]
#remove trailing slash
RewriteRule ^(.*)/$ /$1 [L,R=301]
You want
RewriteRule ^$ /home [L,R=301]
This should catch both http://example.com and http://example.com/, and send them to http://example.com/home

.htaccess 301 Redirect non slash domains and slash domain to .html

I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html

.htaccess, URL changes but loading content

I'm having an issue with .htaccess where I have managed to successfully rewrite the URLs but the content is no longer loading.
The following is my htaccess file.
I'm aiming for all my .html pages (the site is made of .html static pages) to have their extensions removed. However, I require the .html URLs to 301 redirect to the new URL's so that my SEO does not take a hit from these changes.
Example:
Original: www.example.co.uk/page.html
Desired: www.example.co.uk/page/
It is important that the original URL redirects the new URL's though.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(([^/]*/)*[^/.]+)$ $1.html [L]
I have tried the htaccess above and I've also tried the variation below, but neither has worked as desired. Any help would be much appreciated.
Options +FollowSymLinks
RewriteEngine on
# REDIRECT yourdomain.com TO www.yourdomain.com
RewriteCond %{HTTP_HOST} !^www.example.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
RewriteRule (.+)\.html?$ http://www.example.co.uk/$1/ [R=301,L]
You need to change 2 things.
Add this right after your redirect to prevent a redirect loop:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
change the last rule to this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html [L]

Resources