mod_rewrite Multiple Variables - url

Currently my url is mysite.com/content using:
RewriteRule ^page.php/([A-Za-z0-9]+)/?$ page.php?a=b&content=$1 [L]
I'd like to have my url be mysite.com/content/page where both content and page are variables. I thought something like this could work, but obvious it is wrong:
RewriteRule ^page.php/([A-Za-z0-9]+)/?$/([A-Za-z0-9]+)/?$ page.php?a=b&content=$1&page=$2 [L]
What would be the best way to accomplish this?

RewriteRule ^page.php/([A-Za-z0-9]+)/([A-Za-z0-9]+)/?$ page.php?a=b&content=$1&page=$2 [L]
Try this and let me know if this is what you are trying to do.

Related

simple mod_rewrite problem

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^?(.*) productfilter.php?category=$1 [NC,L]
This rewrite is returning :
category=productfilter.php
can someone point out what I am doing wrong here
In all likelihood, the issue is that the rewritten URL is getting re-rewritten. Rewrite directives, at least in .htaccess context, get re-executed after a URL rewrite. I think what you are trying to accomplish can be done with:
RewriteRule ^productfilter.php - [NC,L]
RewriteRule ^([^/]*)/?$ productfilter.php?category=$1 [NC,L]
The first RewriteRule just declines to rewrite when productfilter.php already starts the path.

Stripping the end of a url in the .htaccess file

I have a problem hundreds of 404 errors on my site that are caused by the expressions "URLONCLICK" and "%5C" being somehow inserted at the end of the correct url, and I don't know where it is coming from but I want to just strip it off of the end of the url in the .htaccess file. How do I do that?
i.e.
www.mydomain.com/category/post-title/URLONCLICK
www.mydomain.com/category/post-title/%5C
I want to strip the end off of these urls so they look like:
www.mydomain.com/category/post-title/
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\%5c $ $1 [NC]
RewriteRule ^(.*)\URLONCLICK $ $1 [NC]
This should do. Maybe there is a better way (merging the last two lines).

SEO friendly URLs (.htaccess)

http://www.domain.com/folder/file?x=1&y=2
Change to:
http://www.domain.com/folder/file/1/2/
http://www.domain.com/folder/?x=1
Change to:
http://www.domain.com/folder/1/
I tried:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder/(.*)/$ folder/index.php?x=$1 [L]
RewriteRule ^folder/file/(.*)/(.*)/$ folder/file.php?x=$1&y=$2 [L]
but that doesn't work, does anyone have any idea why?
when i take out the first rule, i can access the second one via:
http://www.domain.com/folder/1/2/
but not:
http://www.domain.com/folder/file/1/2/
god, i hope i am not confusing anyone who is reading this lol i hope it makes sense
Try
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^folder/file/(.*)/(.*)/ /folder/file.php?x=$1&y=$2 [L]
RewriteRule ^folder/(.*)/ /folder/index.php?x=$1 [L]
The order of the rules is important. You should always put the one with the most rules first as your way was getting to the first rule and then stopping because it was always true due to the (.*) which was capturing the file.
Did you try adding a / before the folder name?
RewriteRule ^folder/(.*)/$ /folder/index.php?x=$1 [L]
RewriteRule ^folder/file/(.*)/(.*)/$ /folder/file.php?x=$1&y=$2 [L]

.htaccess RewriteRule to convert directory-type URL structure to a query

i know this question has been asked/answered several times already, but i just cant seem to get past it. so, my apologies...
while in the site dev phase, i want the browsers address bar to display: domain.com/dev/item
i would like the RewriteRule to change it to: domain.com/dev/index.php?p=item
im using the .htaccess located in the public_html directory for this. the .htaccess within public_html/dev, which works fine, is configured to redirect everybody who is not using my ip to domain.com. my apache version is 2.2.15 and i've confirmed that i am able to redirect using .htaccess, but the top level .htacess simply doesnt work. here is the code im using:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^dev/(.*)$ dev/index.php?p=$1 [NC]
#variations tried: ^dev/(.*)+$, ^dev/(.*)+?$
the, including variations, results in a 404, /dev/item not found. removing the dev/ from ^dev/(.*)$ results in the correct url format, but the following server variables:
[REDIRECT_QUERY_STRING] => p=
[REDIRECT_URL] => /404.shtml
[QUERY_STRING] => p=
[REQUEST_URI] => /dev/item
my goal is to make a small change to support this type of rewrite for both the live site and the dev copy. but i just cant understand what im doing wrong. i really would appreciate any help.
You need to specify the / at the beginning of the rewrite paths.
Try this instead:
RewriteEngine on
RewriteBase /
# If request is for a file that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-f
# If request is for a directory that does actually exist, don't rewrite.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/dev/(.*)$ /dev/index.php?p=$1 [NC]

URL rewrite in smarty based website?

I wanted to achieve this
example.com/search_results/?action=search&username[equal]=PorscheSA
to
example.com/PorscheSA
I have used .htaccess for many websites to achieve this, but since this website is smarty based, it doesn't seem to work. Any help will be much appreciated.
Ok, this is the .htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
In order to achieve the following I tried this:
RewriteRule ^([a-zA-Z0-9-_+^/])/$ /search_results/?action=search&username[equal]=$1
and nothing happened.
Your question is still a little vague as to what exactly you expect to have happen, but since you've posted your .htaccess, I'll take a stab at it and see if I get what you were after.
The most obvious reason that your RewriteRule isn't working is that your test pattern doesn't match the URL you've provided as an example, as your RewriteRule requires a trailing slash (and only matches one character before that). Additionally, if you put it after the rules that you currently have in your .htaccess file, it will never match because the request will have already been rewritten to index.php.
I think that you want something like this...
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^.*$ http://example.com/$0 [L,R=301]
# Make sure we end and force the redirect so %{REQUEST_FILENAME} gets changed
RewriteRule ^[a-zA-Z0-9-_+^/]+$ search_results/?action=search&username[equal]=$0 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
However, if the search_results/ directory doesn't exist, then this is just going to be rewritten to index.php anyway, so I'm not sure what the purpose of the redirection would be in that case. And, if it does exist, since your test pattern matches pretty much anything I'd expect to see in a site path, then everything will be rewritten to the search_results/ directory, so very little (if anything) will end up at your site root's index.php.
Based on that, I feel like maybe there's some other criteria that you may have left out, but I could be wrong.

Resources