htaccess Rewrite Rule Exception - php

I have the following code:
RewriteEngine On
RewriteRule ^user/([0-9]+)/([0-9]+)/?$ template.php?user_id=$1&slide=$2 [NC,L,QSA]
So when I go to www.mydomain.com/user/7/1 it is working, and pulling from www.mydomain.com/template.php?user_id=7&slide=1
Now my question is, how do I get the slide variable to be optional. I want it to work if someone will type in /user/7/.
In my template.php file I have:
if(!$current_slide) {
header("Location: user/".$user_id."/1");
}
So if I manually go to template.php?user_id=1 it will automatically redirect to /user/7/1/ which is good, it works. But I need to modify my htaccess file so if someone types in /user/7/ it will redirect to /7/1/
Hope that makes sense.

RewriteRule ^user/([0-9]+)/?([0-9]*)/?$ template.php?user_id=$1&slide=$2 [NC,L,QSA]

Related

Htaccess .html that redirect from query and one not from query

I have problem open an url from page .html extension after i used a hataccess
with the following code
RewriteRule ^(.*)\.html$ single.php?post=$1 [L]
So when i open my page with myhtml.html, the page trying to find from single.php?post=myhtml.html
Please give an idea for better htaccess code that can open .html extension not from the query
Sorry for bad english... Thanks before
When you say ^(.*)\.html, anything you put before .html on your URL it will be assigned to $1. So you should use something like this.
RewriteRule ^myhtml.html$ single.php [L]
RewriteRule ^/myhtml/(.*)$ /single.php?post=$1 [L] //This is a dynamic url
In this example, when you put myhtml.com on your url it will look for single.php and load it and when you say myhtml/any-url.html that any-url will be assigned to $1.
I hope it helps out.

.htaccess redirect subfolders

Im struggling with a .htaccess settings to get following:
To create Multi-language functionality in my CMS without duplicating the productdatabase I would like to get the content of the default language, but keeping the right url in the addressbar.
For example:
http://www.myshop.com/en/webshop get the content of http://www.myshop.com/webshop
http://www.myshop.com/en/collection get the content of http://www.myshop.com/collectie
And also for the products:
http://www.myshop.com/en/webshop/item1 get content of http://www.myshop.com/webshop/item1
http://www.myshop.com/en/collection/product1 get the content of http://www.myshop.com/collectie/product1
In short:
In case of calling the folders '/en/webshop/' and '/en/collection/' there must be a rewrite to '/webshop' and '/collectie'.
Anyone got a clue?
Thanks in advance...
What about:
RewriteEngine on
RewriteRule ^/?en/webshop/(.*)$ /webshop/$1
RewriteRule ^/?en/collection/(.*)$ /collectie/$1
The first line just enables mod_rewrite
The second line (1st rule) matches anything under /en/webshop and discards the /en
The third line is similar to the second but it also "renames" collection to collectie
If you have more folders/paths you want to redirect I 'd suggest adding the rule:
RewriteRule ^/?en/(.*)$ /$1
which is rewriting anything that starts with /en.
Hope it helps, note it is not tested
Use below rule:-
RewriteEngine on
RewriteRule ^en/(.*)$ /$1 [L,R=301,QSA]
OR
RewriteEngine On
RewriteRule ^(.*)/en/(.*)$ $1/$2 [R=301,L]
Hope it will help you :)

.htaccess create user friendly url and ridirect to it

i tryed to search everywhere for this problem but i didnt found nothing.
I want to make make a url seo friendly so i used this code:
RewriteEngine on
RewriteRule ^Homepage index.php [NC,L]
Then i want to redirect to it so i tryed to write this code:
RewriteRule ^index.php$ http://localhost/siti/socialmark/Homepage [R=301,L]
The error it's a loop of redirections, can someone help me?
SORRY FOR MY BAD ENGLISH!
The rewrite rules don't just make the URL string look different, it actually directs the user to the file at the end of the path even if you don't see it in the address bar. If Homepage is a directory containing index.php, even if that php file name doesn't appear in the URL, then it's causing a loop because it's directing you to a directory with an index.php.
The rule is executed every time that page loads. So, you're redirecting to a page which runs the redirect script, so it runs the rule to redirect again, and that causes the loop. What you want to do is create a condition that says "Don't run this code if the requested page is http://localhost/siti/socialmark/Homepage"
Something like this (you may have to adjust it)
RewriteBase /
RewriteCond %{REQUEST_URI} !=/siti/socialmark/Homepage
RewriteRule ^Homepage index.php [NC,L]
For more details, see the caveats and example here:
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_l

Fix broken GET variable

I am using a GET variable to select a specific item to display on the page. My URL would usually look like:
/?type=xxxx
I then have a login plugin (Wordpress) which adds ?redirect=ok to the URL on submission. Unfortunately, this then makes the URL look like: /?type=xxxx?redirect=ok which breaks the previous variable.
I am trying to edit that URL somehow. I thought about a few methods:
.htaccess to find and rewrite the ?redirect=ok to blank or to &redirect=ok
PHP to strip that part out of the string and then redirect
None of these seemed to work.
Any help would be massively appreciated.
Here is mod_rewrite fix for this strange problem.
Put this code in your DOCUMENT_ROOT/.htaccess file as very first rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+([^?]*\?type=.*?)\?(redirect=ok) [NC]
RewriteRule ^ %1&%2 [R=301,L]

RewriteRule help

I have successfully setup htaccess to do this:
domain.com/ad.php?ad_id=bmw_m3_2498224
INTO:
domain.com/ads/bmw_m3_2498224
However, I have a link on the page which makes the page submit to itself...
The link saves the ad inside a cookie:
domain.com/ad.php?ad_id=bmw_m3_2498224&save=1 // Note the 'save' variable
I need to make this work on the rewritten rule also, so this link:
domain.com/ads/bmw_m3_2498224/save
will save the cookie...
I have this so far which DOES NOT work for the save part:
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC,L]
How can I include another rule to accomplish what I want?
Thanks
A simple way would be to add this line above what you currently have:
RewriteRule ^annons/([a-zA-Z0-9_]+)/save$ ad.php?ad_id=$1&save=1 [NC,L]
RewriteRule ^annons/([a-zA-Z0-9_]+)$ ad.php?ad_id=$1 [NC,L]
This should get triggered before the general rule is and work just the same.

Categories