.htaccess not matching - .htaccess

htaccess not matching
RewriteCond %{REQUEST_URI} ^/catalog/products_in_scene.php?(.*)$
RewriteRule ^(.+) "/services/hpv/index.php?%1"
RewriteCond %{REQUEST_URI} ^/shop/derivation_tree.php?(.*)$
RewriteRule ^(.+) "/services/dt/index.php?%1"
The top one matches fine with all the GET variables, the second one matches and sends me to the right page but never sends it the GET variables; Why?

Remove the quote from your RewriteRule.
If it doesn't work, that mean there is something wrong in you php GET variable.

You'd do better to use the [QSA] flag:
RewriteRule ^/shop/derivation_tree.php /services/dt/index.php [QSA]

Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/products_in_scene\.php$ services/hpv/index.php [R,L,NC]
RewriteRule ^shop/derivation_tree\.php$ services/dt/index.php [R,L,NC]

Related

Apache RewriteRule not rewriting as expected

Can you any one see anything wrong with the following apache rewrite rule:
This is in my .htaccess file inside a folder called "text" a subdirectory of localhost/lombardpress I have the following rule
Options +FollowSymlinks
RewriteEngine on
RewriteRule ([^/]+) /textdisplay.php?fs=$1 [NC]
I was expecting this input:
http://localhost/lombardpress-dev/text/lectio1
to rewrite to this:
http://localhost/lombardpress-dev/text/textdisplay?fs=lectio1
But instead I get a 404 error.
The requested URL /textdisplay.php was not found on this server.
It looks to me like the RewriteRule has re-written the address but not as I intended - so there must be something wrong with my regular expression.
Let me know if I can provide further information.
Try this
Options +FollowSymlinks
RewriteEngine on
RewriteBase /lombardpress-dev/text/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/]+) textdisplay.php?fs=$1 [NC]
With that rewrite cond you wont redirect textdisplay.php to itself again.
The problem is that [^/]+ matches all but / so it matches even textdisplay.php
Remove leading slash in target URL:
Try this code:
Options +FollowSymlinks
RewriteEngine on
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ textdisplay.php?fs=$1 [NC,L,QSA]
Reference: Apache mod_rewrite Introduction
Get rid of the / in front of the target:
RewriteRule ([^/.]+) textdisplay.php?fs=$1 [NC]
# no slash here ----^

.htaccess mod_rewrite won't skip RewriteRule with [S]

As an FYI, I am using the following .htaccess file in located at www.site.com/content/
When a user visits www.site.com/content/login I want it to display the content from www.site.com/content/userlogin.php (masked via rewrite, and not redirect) - which I have done SUCCESSFULLY like so:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,L]
However, I would like to add the follwoing: If they try to access www.site.com/content/userlogin.php directly, I want them to get redirected to a 404 page at www.site.com/content/error/404.php
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ /content/userlogin.php [NC,S=1,L]
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
With that in the .htaccess file, both www.site.com/content/login and www.site.com/content/userlogin.php show www.site.com/content/error/404.php
First the S=1 will have no function as the L directive will make any further rewriting stop.
It seems like the first RewriteRule makes Apache go through the .htaccess rules one more time, so you need to know if the first rewrite has happend. You could do this by setting an environment variable like this:
RewriteRule ^login/?$ /content/userlogin.php [E=DONE:true,NC,L]
So when the next redirect occurs the Environment variable actually gets rewritten to REDIRECT_<variable> and you can do a RewriteCond on this one like this:
RewriteCond %{ENV:REDIRECT_DONE} !true
RewriteRule ^userlogin\.php$ /content/error/404.php [NC,L]
Hope this helps
Use %{THE_REQUEST} variable in your code instead:
RewriteEngine On
RewriteBase /content/
RewriteRule ^login/?$ content/userlogin.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+userlogin\.php[\s\?] [NC]
RewriteRule ^ content/error/404.php [L]

.htaccess - RewriteRule

Ok I have the following .htaccess and it works however I can seem to change it to the way I need it.
here is the current .htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
Options +FollowSymlinks
I need to add the following
RewriteRule ^(.*)/(.*)$ /index.php?cat=$1&tpl=$2 [L]
So i am wondering how do I get that rule to work? as when I add it it does not.
So if you want to add a rule that will handle all requests except the first one that matches the RewriteCond And those would be split into /?
I think something like this would work. Please note that I changed the $1 to ${REQUEST_URI} in your rewrite condition
RewriteEngine on
RewriteCond ${REQUEST_URI} !^(index\.php|admin|system|images|tpl|js|lib|favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ /index.php?tpl=$1 [L]
RewriteRule ^([^/]*)/?(.*)?$ /index.php?cat=$1&tpl=$2 [L]
Options +FollowSymlinks

Rewrite rule not working without trailing slash

I have a working rewrite rule to hide index.php?dir= from the URL.So for instance if I try
www.example.com/folder/dir1/
it rewrites it to
www.example.com/folder/index.php?dir=dir1/
and that fine!The trouble is if I remove the trailing slash from the URL i.e.
www.example.com/folder/dir1
it goes into a redirection loop!My complete htaccess is:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /folder
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteCond %{QUERY_STRING} ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
Please advice?
(i) I am confused about the RewriteBase /papers. This only makes sense in DOCROOT/papers/.htaccess. If this the location and is "folder" == papers? If not then, I am not surprised that the rewrite engine is getting confused. (ii) `%{REDIRECT_STATUS} is not 200 on a subquery lookup to evaluate the default if MultiViews or DirectoryIndex is a match.
So before you do anything else:
Validate that your base is correct, and if not fix it.
Use Options -MultiViews if you don't use them.
Check your system, vhost config and DOCROOT/.htaccess to see if a DirectoryIndex is specified. (Unlike rewrite rules which are only taken from the lowest .htaccess, all are scanned for directives such as this.)
Replace the RewriteCond %{ENV:REDIRECT_STATUS} 200 by
RewriteCond %{ENV:REDIRECT_END}%{IS_SUBREQ} true
and add the flag E=END:true to any rules that you want to force to end of the cycle as a match (similar to the Apache 2.4 [END] flag) The extra %{IS_SUBREQ} prevents the rules being fired on a subquery. You don't want this to happen unless you really know what you are doing.
Figured it out!
Had to replace
RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]
with
RewriteRule ^(.*)? index.php?dir=$1/ [L,QSA]
Thanks everyone for contributing..

.htaccess mod_rewrite playing with variables

I want to have my site urls look like
http://example.com/place/info?var=info&morevars=ifneeded
Place and info are also variables but they have a fixed name, the ones after would vary. EDIT This is the url I am trying to rewrite
http://example.com/test.php?place=test&action=info&var=info&morevars=ifneeded
This is what I have so far
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC]
I think there a way to do this with {QUERY_STRING} but I can't get it to work just 500 errors or it don't make a differences.
You have set the QSA flag that automatically appends the original requested query to the new one:
RewriteEngine on
RewriteRule ^([A-Za-z0-9-]+)/?$ test.php?place=$1 [NC,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ test.php?place=$1&action=$2 [NC,QSA]
You're missing the first /
RewriteEngine on
RewriteRule ^/([A-Za-z0-9-]+)/ test2.php?place=$1 [NC]
RewriteRule ^/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/ test2.php?place=$1&action=$2 [NC]

Resources