Apache RewriteRule not rewriting as expected - .htaccess

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 ----^

Related

.htaccess and rewrite rule 404 not found

I'm new on .htaccess and rewrite rules.
So if my question is not relevant, please forgive me.
I have below htaccess code.
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
If i visit url like www.example.com/category/0 it works.
But if i strip page url and last slash www.exapmle.com/category i see an ugly 404 page.
What is wrong with my htaccess directive?
Thanks in advance.
EDIT: Compeletely .htaccess
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
## If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
## If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
## If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
## don't do anything
RewriteRule ^ - [L]
RewriteRule ^haber/([^/]+)-([^/]+)/?$ article.php?link=$1&i=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L,QSA]
This is a correct behaviour.
Actually, in a regular expression, a + means at least one.
When you use ([^/]+) it means at least one character which is not a slash.
Your rule ^([^/]+)/([^/]+)/?$ means at least one character which is not a slash / at least one character which is not a slash optional slash.
That's why it does not work with only the first part url.
If you also want to handle example.com/category you'll need another rule:
RewriteRule ^([^/]+)/?$ article-list.php?link=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ article-list.php?link=$1&page=$2 [L]

htaccess redirection for double slashes

I am having a problem with my site. Sometimes after the user log in is redirected to a page like this:
mydomain.com//somepage
Please notice the double slash in the URL, which takes to a not valid page so i want to modify my htaccess in order to make all pages with double slash to automatically redirect to an URL like this:
mydomain.com/folder/somepage
Please notice the word "folder" between slashes this time. An URL like this would always take to a valid page.
I made some rewrite rules for my htaccess but they dont work as expected:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
</IfModule>
Could you please give me a hint?
Thank you.
Try something like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ //([^\?\ ]*)
RewriteRule ^ /folder/%1 [L,R=301]
You need to match against the %{THE_REQUEST} variable because the URI gets normalized before it gets matched against the pattern of rewrite rules.

Rewrite rule not working as expected?

I have a URL with a parameter which I wish to make into sef URL:
want:
http://map.tautktiv.com/street.php?address=abc
to become:
http://map.tautktiv.com/street/address/abc
or
http://map.tautktiv.com/address/abc
have tried several online tools to generate a .htaccess rule, but none of them have any effect on the URL, .htaccess file is active (tried to put some gibberish in it and got error 500)
these are the rules I tried:
1.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^address-([^-]*)$ /street.php?address=$1 [L]
RewriteRule street/address/(.*) street.php?address=$1
2.
Options +FollowSymLinks
RewriteEngine on
RewriteRule /address/(.*)\.php street.php?address=$1
3.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# add whatever other special conditions you need here
RewriteRule ^/([0-9]+)-(.*)$ /street.php?address=$1 [L]
RewriteRule /(.*)/(.*)/$ street.php?address=$1
the site is a sub-domain which files reside in a sub directory in a shared hosting GoDaddy server, have also tried to apply these rules to the .htaccess in the directory above it, same result.
tried also this per below suggestions
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
same result, nothing happens.
tried to go directly to page from main domain but same result:
http://tautktiv.com/map/streets/street.php?address=abc
First rule will redirect your ugly URL to the pretty URL.
Second rule will internally redirect it back so the user will not see the ugly URL.
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
# Internally forward /street/address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^street/address/(.*)/?$ /street.php?address=$1 [NC,L]
# Internally forward /address/abc to /street.php?address=abc
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^address/(.*)/?$ /street.php?address=$1 [NC,L]
If you confirm the rule to be working as expected then you can change it from 302 to 301 as you do not want to use 301 until you know the rule is working as expected.
The .htaccess should go inside the folder where street.php is located.
HTTP is US ASCII so your language would fail, it will redirect it to something like this:
/street/address/%25D7%2590%2520%25D7%2598%25D7%2591%25D7%25A8%25D7%2599%2520%25D7%2599%25D7%25A8%25D7%2595%25D7%25A9%25D7%259C%25D7%2599%25D7%259D%2520%25D7%2599%25D7%25A9%25D7%25A8%25D7%2590%25D7%259C
Your best bet here would be to change the links to use /street/address/word instead of the php file directly.
This way you would not need the first rule and you can use only the internal redirect which would work just fine with this update.
Try this one:
RewriteEngine on
RewriteRule ^street/address/(.*)$ street.php?address=$1 [r=301,L]
RewriteRule ^address/(.*)$ street.php?address=$1 [r=301,L]
In your examples you'd missed ^ and $ in the second row of RewriteRule.
And use [r=301,L] instead of [L] to tell the browser, that thzis is premanent redirecting.

.htaccess not matching

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]

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..

Resources