Mod Rewrite Loses Query Parameters - mod-rewrite

The following rewrite rules are being used .htaccess on a simple PHP site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /test/
RewriteRule ^index\.php$ - [L]
RewriteRule ^([a-zA-Z-]+)/?$ /test/index.php?m=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /test/index.php [L]
</IfModule>
The following URLs work properly:
domain.com/test/index.php?m=profile
domain.com/test/profile
domain.com/test/profile/
However, additional query parameters are lost with the rewrite:
domain.com/test/index.php?m=profile&id=2000 ==> works
domain.com/test/profile?id=2000 ==> does not work
domain.com/test/profile?id=2000 ==> does not work
Any help appreciated.

Change this line:
RewriteRule ^([a-zA-Z-]+)/?$ /test/index.php?m=$1 [L]
to include the QSA flag:
RewriteRule ^([a-zA-Z-]+)/?$ /test/index.php?m=$1 [L,QSA]
Which tells apache to append the existing query string to the new one (m=$1).

Related

How to properly configure the .htaccess of Typo3 to run realurl

I'm a newcomer to typo3 and my current task is to get realurl working. After studying many tutorials and the official documentation I got realurl to the point that the links are converted correctly as expected.
The problem I don't get solved even after testing multiple solutions is the wrong behaviour of the apache server (configured via .htaccess, mod_rewrite). It correctly ignores the rewrites to uploads/ fileadmin / typo3conf but never rewrites the url to index.php when it comes to typo-pages.
Here is the corresponding .htaccess entry:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^typo3$ - [L]
RewriteRule ^typo3/.*$ - [L]
RewriteRule ^uploads/.*$ - [L]
RewriteRule ^fileadmin/.*$ - [L]
RewriteRule ^typo3conf/.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (.*[^/])$ http://%{HTTP_HOST}/$1/ [L,R]
RewriteCond %{REQUEST_FILENAME}/index.html -f
RewriteRule / %{REQUEST_URI}/index.html [L]
RewriteCond %{REQUEST_FILENAME}/index.htm -f
RewriteRule / %{REQUEST_URI}/index.htm [L]
RewriteCond %{REQUEST_FILENAME}/index.php -f
RewriteRule / %{REQUEST_URI}/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule * /index.php
</IfModule>
Does anybody have an idea whats wrong with this configuration? I'm some kind of desperate :(
Thanks in advance,
Thomas
Best solved with a rewrite log: https://wiki.apache.org/httpd/RewriteLog

mod rewrite how do i add multiple rules with different combinations

i have been pulling my hair out for 3 days. i ve checked forums and tutorials but i just dont get it. i have a url of
http://localhost/gallery/2012/2
but i also need to get
http://localhost/gallery/2
this is my htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/([0-9-]+)/?$ index.php?page=$1&date=$2&pagenum=$3 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/?$ index.php?page=$1&date=$2 [L]
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?page=$1 [L]
</IfModule>
how would i go about adding another rule
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9-]+)/?$ index.php?page=$1&pagenum=$2 [L]

How do you remove index.php from a url using mod_write, while still allowing php to see the path with index.php in it?

For the past 3 days I have been playing around with Apache's mod_rewrite trying to get it to remove index.php from my url, while php still needs to see it in the path.
Essentially PHP needs to see this
http://example.com/index.php/Page/Param1/Param2
While the user needs to see this
http://example.com/Page/Param1/Param2
What I have right now is the following in an htaccess file
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Which was taken from another page, and is close to what I need. However this seems to be cutting off everything after the http://example.com/ part. How can I get mod_rewrite to show the user one thing and have php see something else?
This is the modified code you can use in your .htaccess (under DOCUMENT_ROOT) to remove index.php from URI:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^index\.php)^(.+)$ /index.php/$1 [L,NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php(/[^\s\?]+)? [NC]
RewriteRule ^ %1%2 [R=302,L]
Change R=302 to R=301 once you're satisfied that it is working fine for you.
This rule:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Needs to look like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\ /index\.php(.*)\ [NC]
RewriteRule ^ /%1 [R=301,L]
Also note that RewriteRule ^(.*)$ index.php?$1 [L,QSA] doesn't create a URI that looks like this /index.php/Page/Param1/Param2, it creates a query string that looks like this: /index.php?Page/Param1/Param2. Which isn't at all what you said PHP needs to see.

Removing Trailing Slash with .htaccess

I am trying yo remove the trail slash from an url with this pattern
http://localhost/~francesco/mycms/about/
to make it
http://localhost/~francesco/mycms/about
I have tried lots of rules but no one is working for me.
My rewrite rule is this by now
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~francesco/mycms/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
</IfModule>
Hope someone can help me!
Try placing this rule
RewriteRule ^(about)/$ $1 [L,R=301,NC]
just after
RewriteBase /~francesco/mycms/
If you want to work for any first level directory following the RewriteBase i.e. http://localhost/~francesco/mycms/[anything-here]/ use
#exclude the /~francesco/mycms/admin/directory
RewriteCond %{REQUEST_URI} !^/admin/ [NC]
RewriteRule ^([^/]+)/$ $1 [L,R=301,NC]

Question about url rewriting using apache mod_rewrite

I have a doubt about url rewriting using apache mod_rewrite. I am a newbie in mod_rewrite and I don't have any experience in regex.
What I want to do is to:
Rewrite / To /web/content/public/
Rewrite /clients/ To /web/content/clients/
How can I achieve above things.
I tried:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^(.*)$ web/content/public/$1 [L]
</IfModule>
But it doesn't work. What can I do?
^(.*)$ includes the slash. So don't include the slash in the rewritten pattern.
But include a root slash at the head of your rewritten pattern.
RewriteRule ^/clients(.*)$ /web/content/clients$1 [L]
RewriteRule ^(.*)$ /web/content/public$1 [L]
Check the apache access log and error log to see what kind of request URL comes back.
Please check the documentation, especially the list labeled "Here are all possible substitution combinations and their meanings:"
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^/clients/$ web/content/clients/ [L]
RewriteRule ^/(.*)$ web/content/public/$1 [L]
And if you want to use that rules in a .htaccess file, remove the leading slash from the patterns.

Resources