Rewrite Rule in htaccess all works except the last rule - .htaccess

Mine actual code is this and the last rule dsnt work properly, someone can help please?,
A lot of thanks!
RewriteEngine On RewriteCond %{HTTP_HOST} ^diamantesgould.net [NC]
RewriteRule ^(.*)$ http://www.diamantesgould.net/$1 [R=301]
RewriteRule ^([a-z0-9_-]+)$ /index.php?po=$1 [NC]
RewriteRule ^categories/([a-z0-9_-]+)$ /index.php?ca=$1 [NC]
RewriteRule ^videos/([a-z0-9_-]+)$ /video.php?movie=$1 [NC]
RewriteRule ^galleries/([a-z0-9_-]+)$ /galleries.php?gallery=$1 [NC]
NOTE: The problem is that it shows the same page by typing mydomain.com/galleries or mydomain.com/galleries/1 or mydomain.com/galleries/1/2/3/4 ... It always shows the galleries page (mydomain.com/galleries)

I see something that may not be rigth in your regexp:
[a-zA-Z0-9_-]+ should be [a-zA-Z0-9_\-]+, - is a special char so must be escaped to be used only as a char

Related

htaccess replacing underscores with hyphens

Is there a more efficient way to doing this?
The last /(.*)$ is an ID that I don't care to use. only whats before it.
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4-$5-$6-$7-$8-$9-$10 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4-$5-$6-$7-$8-$9 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4-$5-$6-$7-$8 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4-$5-$6-$7 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4-$5-$6 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4-$5 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3-$4 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2-$3 [NC]
RewriteRule ^about-us/news-room/(.*)_(.*)/(.*)$ index.php?go=/news/press-releases/$1-$2 [NC]
I found some solutions online but seem to get really confused on using the [N] flag? Not too sure here. Can anyone explain a better more efficient way to do this?
You can just let the rewrite engine loop internally for this:
RewriteRule ^about-us/news-room/(.+)/(.*)$ index.php?go=/news/press-releases/$1 [L,NC]
RewriteCond %{QUERY_STRING} ^go=/news/press-releases/(.*)_(.*)$
RewriteRule ^index\.php$ /index.php?go=/news/press-releases/%1-%2 [L]
The first rule sends the request to index.php, and the second rule removes the underscores and replaces them with dashes. Because the rewrite engine loops, it'll keep applying the rule until either the recursion limit is reached or all the underscores are gone.

HtAccess rewriting, removing a part of the text between the first two slashes and adding index.php after it

I got the following url:
127.0.0.1/abc_123456/default/index/index/
Which should be rewritten to:
127.0.0.1/123456/index.php/default/index/index/
So remove abc_ and add index.php after it. Problem is that the digits are variable, but the abc_ isn't.
I had the following rule:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_
RewriteRule ^abc_(.*)/(.*)$ /$1/index.php/$2
But that resulted in the url being rewritten to:
127.0.0.1/123456/default/index/index.php/index/
Seems like I'm almost there, but I can't figure it out.
Thanks in advance
Use this simple rule:
RewriteRule ^abc_([0-9]+)/(.*)$ $1/index.php/$2 [L,NC]
Try
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /abc_([0-9]+)/([^\ \?]+) [NC]
RewriteRule ^ /%1/index.php/%2 [L]
EDIT
However, you are right about the other rule, that's the one giving the error; RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ RewriteRule ^(.*)$ /index.php/$1 That one is used to rewrite if the page does not contain the /abc_123456/
Add an extra condition to that rule as below
#if not abc_
RewriteCond %{THE_REQUEST} !^[A-Z]+\ /abc_ [NC]
#if not already index.php
RewriteCond %{REQUEST_URI} !^/index\.php$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
for example, if you need similar solution, when visited this url:
http://yoursite.com/subpage1/subpage2/?YOURSTRING=blabla
to redirected visitor to
http://yoursite.com/subpage1/subpage2/
then see link - http://stackoverflow.com/a/15680832/2215124

Got a small htaccess error in my code

I have an htaccess code block that works fine well except under one particular condition:
One, I add a 'www' when there isn't one, that works.
Two I chop off 'index.html' when that appears.
The problem occurs, when I have NO 'www' AND "index.html' on the end.
I get the www added and then the index is chopped off and a whole URL string is added
thereby duplicating my URL and causing a 404.
http://www.example.com/http://www.example.com
Here is my block whats wrong?
RewriteCond %{HTTP_HOST} !^www\.kisswedding\.com$ [NC]
RewriteRule ^(.*)$ http://www.kisswedding.com/$1 [L,R=301]
#RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
#RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE]
I am sure its just a simple thing but I am too dang tired to see it so I ask you guys :)
L
I think you need to add the L flag, so that Apache will be told to stop rewriting in that rule
RewriteCond %{HTTP_HOST} !^www\.kisswedding\.com$ [NC]
RewriteRule ^(.*)$ http://www.kisswedding.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} /index\.html?$ [L,NC]
RewriteRule ^(.*)index\.html?$ "/$1" [L,NC,R=301,NE]

How to use .htaccess to redirect a url which has a # character

I've been looking at this for a couple of days, even had the IT guys at work try and figure it out but we didn't get very far.
I have a url my.website.com/index.php#!lightBox[gallery]/0/ which I want to redirect to just my.website.com. I can do it fine with just the index.php, but it's the extra parts that aren't working. I've tried:
RewriteCond %{HTTP_HOST} ^my.website.com$ [NC]
RewriteRule (.*) http://my.website.com/index.php#!lightBox[gallery]/0/? [R=301,L]
and cpanel generated the following redirect:
RewriteCond %{HTTP_HOST} ^my.website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.my.website.com$
RewriteRule ^/?$ "http\:\/\/my\.website\.com\/\#\!lightBox\[gallery\]\/0\/" [R=301,L]
Both output my.website.com/%23!lightBox[gallery]/0/ as the url - as you can see the hash isn't being processed.
If anyone has any ideas I'd love to hear!
Thanks :)
Have your rules like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com$ [NC]
RewriteRule ^/?$ http://my.website.com/index.php#!lightBox[gallery]/0/ [R=301,L,NE]
You do need to escape dot . in RewriteCond but don't need to escape any special characters on RHS. Importantly you need to have flag NE to not to escape the resulting URI.

Help with .htaccess RewriteRule. Need to take user from one URL to other

I want users who type
http://www.example.com/word-of-the-day
to be taken to
http://www.example.com/index.php?page=word-of-the-day
But I want
http://www.example.com/word-of-the-day
to be shown in the URL for the user.
What should I do in my .htaccess? I tried but the regular expression
and the syntax of RewriteRule is way too complicated for me to
figure out how to do it.
Any help will be appreciated.
Edit:
Also, how can I say this in htaccess -
if they type http://www.example.com/word-of-the-day, take them to http://www.example.com/index.php?page=word-of-the-day
or if they type http://www.example.com/something-else, take them to http://www.example.com/index.php?page=something-else
or else, just take them to the URL they typed.
The condition below checks that index.php is not being requested. If not apply the rule. This will work for any of the scenarios you listed above.
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteRule ^(.*)$ index.php?page=$1 [L]
In response to your comment about only wanting to do this for a few specific pages, it would look like this(as an alternative to Nils edit):
RewriteCond %{QUERY_STRING} ^!.*[index\.php].*$ [NC]
RewriteCond %{REQUEST_URI} ^word-of-the-day$ [OR]
RewriteCond %{REQUEST_URI} ^something-else$ [OR]
RewriteCond %{REQUEST_URI} ^even-something-else$
RewriteRule ^(.*)$ index.php?page=$1 [L]
Try this
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
Or more flexible
RewriteEngine on
RewriteRule ^(.*)$ index.php?page=$1
Not tested, yet it sould work.
To your edit:
Just define those specific URLs manually:
RewriteEngine on
RewriteRule ^word-of-the-day$ index.php?page=word-of-the-day
RewriteRule ^word-some-example$ index.php?page=some-example
RewriteRule ^some-other$ index.php?page=some-other

Resources