htaccess and friendly urls with more than one parameter - apache

I have no experience working with htaccess, so i have this url: index.php?id=1163&sub=hp
i have managed to convert the first portion into a friendly url with htaccess:
www.mytesturl.com/index.php?id=1163 into www.mytesturl.com/computers/
however i need to convert the whole url (two parameters) for example:
www.mytesturl.com/index.php?id=1163&sub=hp into www.mytesturl.com/computers/hp/
here is my htaccess:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?perf=$1
Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mytesturl.com
RewriteRule (.*) http://www.mytesturl.com/$1 [R=301,L]
</IfModule>
can someone please point me into the right direction? the second instruction should be on the same line or its necesary to add another rule or directive
similar to: RewriteRule (.*) http://www.mytesturl.com/$1 [R=301,L]

You can possibly use:
RewriteEngine On
RewriteRule ^([\w-]+)/?$ index.php?perf=$1 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?perf=$1&sub=$2 [L,QSA]
But remember your rules are using ?perf=... as first parameter not ?id=... as your question says.

Related

htaccess RewriteRule for 2 parameters

I need some help with .htaccess RewriteRules. Using index.php to parse parameters and render a needed page.
I have a link domain.com?ac=221&co=100. It needs to be accessed by url domain.com/221/100
I've already made a RewriteRule for ?ac -
RewriteRule ^([^/d]+)/?$ index.php?ac=$1 [QSA]
How can I add a support for second argument?
Possible url variants:
domain.com
domain.com?ac=221
domain.com?ac=221&co=100
Options -MultiViews
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?ac=$1&co=$2 [NC,QSA,L]
RewriteRule ^([a-zA-Z0-9]+)$ index.php?ac=$1 [NC,QSA,L]

Redirect using mod_rewrite in file .conf is not working

In my server apache, I need to redirect URLs in a .conf file. In my file http.conf, located in /etc/httpd/conf, one of the lines contains the following configuration:
Include conf.d/*.conf
In the folder the setting is indicating, in /etc/httpd/conf.d, I have a file called redirect.conf that has the following redirect code inside:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^/?abracar/?$ /abracar? [L,R=301]
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^/?presente/decoracao/teu-sorriso/?$ /presente/decoracao/teu-sorriso? [L,R=301]
RewriteCond %{QUERY_STRING} ^p=2&PageSpeed=noscript$ [NC]
RewriteRule ^/?acessorios/confeitaria.html/?$ /acessorios/confeitaria.html? [L,R=301]
</IfModule>
This code should redirect the URL https://example.com/abracar?PageSpeed=noscript to https://example.com/abracar, but it is not working. If I put this code in my .htaccess file, it will work correctly, but due to external factors, I need to put this redirect into a separate .conf file. I have tried several different configurations, I researched a lot about it already, but nothing that could help me solve this problem. Thanks in advance!
I guess the easiest solution would be to drop the query string entirely?!
Note that QSD requires Apache 2.4 or newer.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} ^PageSpeed=noscript$ [NC]
RewriteRule ^ %{REQUEST_URI} [QSD,L,R=301]
</IfModule>
Added a more generalized and easier to comprehend (for humans) version:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [QSD,L,R=301]
</IfModule>
.+ is the generalized form of and query string that isn't NULL. and the RewriteRule now lists all parts of the URL, but I'd stick to ^ %{REQUEST_URI} for the real version, because both versions just rewrite every URL to itself.

HTACCESS: Redirect VS RedirectMatch to redirect one page to another on same domain?

I have my HTACCESS file here (below), and I want to redirect /payments to /design/payments just to make it a little easier when sending the link to clients.
Here is the whole file:
<Files .htaccess>
order allow,deny
deny from all
</Files>
ErrorDocument 404 http://mrobertsdesign.ca/404
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^mrobertsonline\.ca [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.mrobertsonline\.ca [NC]
RewriteCond %{HTTP_HOST} ^www\.mrobertsdesign\.ca [NC]
RewriteRule ^(.*)$ http://mrobertsdesign.ca/$1 [L,R=301,NC]
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?category=$1&page=$2 [NC]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?category=$1&subcat=$2&page=$3 [L,NC]
The file works fine, if you see anything that should be changed, please let me know as well. But it works fine, any ways...
To redirect the page "/payments" to "/design/payments", I understand I should use "redirect" as follows:
Redirect 301 /payments /design/payments
But when I add that, it redirects me to:
http://mrobertsdesign.ca/design/payments?page=payments
Now I don't understand what exactly I need to change, that's all really, all help would be appreciated!
You need to add a blocking condition before your rule
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [NC]
She gets fired with a request to /payments, adds the parameter and then the Redirect keeps those added parameters.
RewriteCond %{REQUEST_URI} !^/payments
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [NC,QSA]
As a side note may be you also want to add the [QSA][1] flag in the last 3 rules, because when you add parameters in a RewriteRule, the previous query string gets lost.

.htaccess is concatenating rules

I have a .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
this is working for http://www.domain.com/frases/13571/
The point is that I need to redirect all HTTP to HTTPS after this rule is already applied.
So I write this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But isn't working like I supposed to. I'm getting this when I perform a HTTP request:
https://www.domain.com/frases/13571/?categoria=frases&id=13571
What is the right code that just change the protocols without modify the others parts of url?
I think that my code is doing 2 requests in the first rules and that is generating this wrong url.
L flag must be applied to your rewrite rule.
You can use this code instead
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
Note:
you don't need to escape slashes (\/ can be /)
you'll need to clear your browser's cache before trying again with this code. Your old rule is now in cache and you won't see the new code working if you don't clear it.
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You rewrite from the original REQUEST_URI, the current is what you have captured, so try:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
or
RewriteRule (.*) https://$1 [R=301,L]
If not working, rewrite log could be useful.

apache rewritemap not working URL does not translate

I cannot get my rewrite map to work. I have read a lot of pages on this and yet can't figure out how.
I have the following in my httpd.conf and took an apache restart
RewriteMap ErrorMap txt:/usr/local/apache/conf/userdata/std/1/techencl/site.com/vb2xf.map
The rewrite map has the following entry (edited after the tip in the first reply)
corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/
The .htaccess in the root directory has the following entry
RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
Edit: Just posting my current .htaccess file contents for reference
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule [^/]+/.+-([\d]+)/p([\d]+)/ showthread.php?t=$1&page=$2 [NC,L]
RewriteRule [^/]+/.+-([\d]+)/ showthread.php?t=$1 [NC,L]
Options +FollowSymlinks
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
RewriteCond ${ErrorMap:$1|NOT_FOUND} !NOT_FOUND
RewriteRule ^(.*)$ ${ErrorMap:$1|$1} [R=301]
</IfModule>
When you call the map:
${ErrorMap:$1
The $1 is never going to have http://www.site.com/ in it. It will only have this part:
corner/lock-unlock-140859.html
That means in your map, you need to make it look like this:
corner/lock-unlock-140859.html http://www.site.com/com/ts/lock-unlock.70898/