Basic htaccess code not working - .htaccess

This is really frustrating for me that I have been trying to get this very basic htaccess rewrite working from hours long but couldn't do it.
I have tried following.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /funshoppie/
############ Details page ################
RewriteCond %{THE_REQUEST} Deal\-details\.php\?title\=([A-Za-z0-9-]+)&id=(\d+) [NC]
RewriteRule ^%1%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^Deals/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ Deal-details.php?title=$1&id=$2 [NC,QSA,L]
can anyone tell me what wrong i am doing?
url i am trying to rewrite
http://localhost/funshoppie/Deal-details.php?title=Styling%20Tools%20-%2020%%20off&id=7
what i wish after success
http://localhost/funshoppie/Deals/Styling-Tools-20%-off/7
I have checked error logs, httpconf & ensured uncommented rewrite_module. Don't know whats causing this error.

I cannot try this at the moment but, your regular expression is not fitting the URL:
Deal-details.php?title=Styling%20Tools%20-%2020%%20off&id=7
Based on en- and decoding this could be the following two string:
Deal-details.php?title=Styling%20Tools%20-%2020%%20off&id=7
Deal-details.php?title=Styling Tools - 20% off&id=7
In none of the cases matches your expression:
[..] Deal-details.php\?title\=([A-Za-z0-9-]+)&id=(\d+)[..]
You are missing represented characters from your expressions like:
"%"
" " (space)
Try the following expression:
Deal\-details\.php\?title\=(.+)&id=(\d+)
This will match any characterafter title. Otherwise I see no problem there.

Finally i figured it out by my own i believe.
There was some trailing slashes issues in the last rewrite rule due to it was not doing the intended. Further i rewritten all existing & future urls via some clean url function & used those pretty statements in url. Then it worked !!
RewriteCond %{THE_REQUEST} Deal\-details\.php\?title\=([A-Za-z0-9-_]+)&id\=(\d+) [NC]
RewriteRule ^ %1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-_]+)/(\d+)$ Deal-details.php?title=$1&id=$2 [NC,QSA,L]
This worked for me.

Related

htaccess SEF URLS dont work with the word 'drills'

I have a strange problem where my SEF URLs just wont work for the word 'drills' I have just got around the issue by using a different word but Id like to know why this doesn't work.
This is my entire htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9])/?$ /index.php?page=$1 [QSA,NC,L]
This works perfectly and redirects domain.com/page/1 to the correct domain.com/index.php?page=1
However, if I change the htaccess file to this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^drills/([0-9])/?$ /index.php?drill=$1 [QSA,NC,L]
I just get a url not found error.
To find out where it was going wrong I edited the htaccess 1 letter at a time so page became drills and it all worked correctly until I changed the initial letter. So this works:
RewriteRule ^prills/([0-9])/?$ /index.php?drill=$1 [QSA,NC,L]
but this is a URL not found:
RewriteRule ^drills/([0-9])/?$ /index.php?drill=$1 [QSA,NC,L]
Is there something special about the word 'drills' that means I can't use it for SEF URLs?
To fix it I've just used training-drills instead but it's bugging me that I couldn't get it to work with just the word 'drills'

Remove parenthesis from htaccess (Helicon Ape)

I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

.htaccess RewriteRule Change Directory To Query String 500 Internal Server Error

I have the following code in my .htaccess file, which is mean to convert a directory to a query string parameter:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^g/XXXXXXXXXX-WINEGIFTS1$ g/index.php?u=XXXXXXXXXX-WINEGIFTS [L,QSA]
RewriteRule ^g/(.*)$ g/index.php?u=$1 [L,QSA]
So, http://example.com/g/SOMETEXT should get rewritten as http://example.com/g/index.php?u=SOMETEXT
As it stands, it works fine.
However, if I uncomment out the commented line, I get a 500 Internal Server Error.
That line is meant to rewrite one specific URL, http://example.com/g/XXXXXXXXXX-WINEGIFTS1, as http://example.com/g/index.php?u=XXXXXXXXXX-WINEGIFTS
The weird thing is that I have the exact same code running on a different domain, on a different server, and it works fine.
Any thoughts on what's causing this error?
Thanks!
The problem is that if you uncomment the first rule then the second rule becomes unconditional and it matches /g/index.php twice and rewrites to inself causing an endless rewrite loop .
You can use the following
Options +FollowSymLinks
RewriteEngine On
##--Skip the all rules bellow if an existent file/dir is requested--##
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
#########
RewriteRule ^g/XXXXXXXXXX-WINEGIFTS1$ g/index.php?u=XXXXXXXXXX-WINEGIFTS [L,QSA]
RewriteRule ^g/(.*)$ g/index.php?u=$1 [L,QSA]

.htaccess RewriteRule not working with querystrings as directory

I have the following code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
RewriteRule ^([^\.]+)/([^\.]+)/$ $1.php?id=$2
I had problems with with the absolute URI, it solved. Now I want to use the last row for the following:
domain.com/query/ping/2/
to
domain.com/query/ping.php?id=2
I think the code is good, but I still get back error 404. Should I give some rewrite conditions? I searched for this code but haven't found anything useful.
Based on where you've placed your htaccess file in your previous question, you probably want something like this:
RewriteCond %{REQUEST_URI} ^/query/([^/]+)/([^/]+)/?
RewriteCond %{DOCUMENT_ROOT}/query/%1.php -f
RewriteRule ^ /query/%1.php?id=%2 [L]
The important issue is that you need to check for /query in the beginning of the URI. What you have, ^([^\.]+)/([^\.]+)/$ won't match the /query part along with the 2 pathnames after it. Your regex only matches 2 pathnames.
The line RewriteCond %{DOCUMENT_ROOT}/query/%1.php -f is similar to the condition you have above where it checks to see if the requested PHP file actually exists, otherwise it won't blindly rewrite. This condition ensures if someone tries to go to:
http://domain.com/query/blahblahblahblah/blah
Your server won't return a 404 error saying /query/blahblahblahblah.php doesn't exist.

Redirect Rule not working

I am trying to redirect wordpress default login (wp-logiin.php) url to signin but somehow it is not working. I have never done this .htaccess rewrite rule before so no idea how it works.
# BEGIN WordPress
RewriteRule ^signin$ http://localhost/newsite/wp-login.php [NC,L,R]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /newsite/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /newsite/index.php [L]
</IfModule>
# END WordPress
Big Big thanks
Try to read manual :)
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
First of all, if you want to use "rewrite", condition must be defined first, followed by rule which is used if is condition met. For example:
RewriteCond %{HTTP_USER_AGENT} ^facebookexternalhit [OR,NC]
RewriteCond %{HTTP_USER_AGENT} ^Zeus [NC]
RewriteRule ^.* /errors/404.html [F]
RewriteRule ^signin$ http://localhost/newsite/wp-login.php [NC,L,R]<br>
Use of RewriteRule without defined RuleCondition, thus IMHO this rule will not be used, never.
RewriteCond %{REQUEST_FILENAME} !-f<br>
RewriteCond %{REQUEST_FILENAME} !-d<br>
RewriteRule . /newsite/index.php [L]<br>
As say manual "Pattern is a perl compatible regular expression.", thus single "." as pattern mean one single character. So, if request filename will be "test.txt", it will replace every letter with "/newsite/index.php" (8 chars = 8x repeat).
Correct version is: RewriteRule ^.*$ /newsite/index.php [L]
You're redirecting logins to localhost, so unless you're using a browser from the same machine that your wordpress site is running on, you're probably going to get a connection error. Try removing the http:// bit from your rule:
RewriteRule ^signin$ /newsite/wp-login.php [NC,L,R]

Resources