.htaccess to shorten current url - .htaccess

Right now I have direction to:
www.website.com/user.php?u=peter
or
www.website.com/userhome.php?u=peter
I want to shorten them to
www.website.com/user/peter
www.website.com/userhome/peter
This is the code I have now, and its not working. When I type website.com/user/peter the website gives me an my own error, as if it isn't able to GET the username. "That user does not exist or is not yet activated, press back"
# Turn Rewrite Engine On
RewriteEngine on
# NC make the rule non case sensitive
# L make this the last rule that this specific condition will match
# $ in the regular expression makes the matching stop
# Rewrite for user.php?u=xxxxx
RewriteRule ^user/([0-9a-zA-Z]+) user.php?u=$1 [NC,L]
# Rewrite for userhome.php?u=xxxxx
RewriteRule ^userhome/([0-9a-zA-Z]+) userhome.php?u=$1 [NC,L]

Try using the following rules:
#To leave the URL www.example.com/user/peter
RewriteRule ^user/([^/]*)$ /user.php?u=$1 [L]
#To leave the URL www.example.com/userhome/peter
RewriteRule ^userhome/([^/]*)$ /userhome.php?u=$1 [L]
Make sure you clear your cache before testing these.

Related

.htaccess rewrite dynamic url problems

I know there is lots of rewrite topic but i coulndt get it. So I try to rewrite url with htaccess. But I don't know very well about rewrite rule so i want your helps.
rewrite index.php to root(it works).
www.example.com/index.php ---> www.example.com
RewriteRule ^(.*)index\.(php|html?)$ /$1 [R=301,NC,L]
After index.php to root rewrite my dynamic url change from this
https://example.com/index.php?baslik=dynamic-slug-url
to this
https://example.com/?baslik=dynamic-slug-url
Php codes(fetch etc.) work fine and no problem. But I want to change into this(I tried dynamic url rewrite generator but no result):
https://example.com/dynamic-slug-url
After the above things, I want to change something else. From this;
https://example.com/specificfile.php?target=dynamicid
to this
https://example.com/dynamicid
When it became upper thing,
https://example.com/specificfile.php?target=dynamicid&baslik=dynamic-slug-url
Will be
https://example.com/dynamicid&baslik=dynamic-slug-url
But I want like this
https://example.com/dynamicid/dynamic-slug-url
I hope i told my problem and other things clear. I tried lost of url rewrite generator but couldnt get no result. Thank you for your answer and good explanation.
I used that problem as a little exercise for myself and this is what I came up with:
# 1) from https://example.com/index.php?baslik=dynamic-slug-url
# to https://example.com/dynamic-slug-url
# and
# 2) from https://example.com/specificfile.php?target=dynamicid
# to https://example.com/dynamicid
RewriteCond %{QUERY_STRING} ^(baslik|target)=([^/.&]+)$
RewriteRule ^(.*)(index|specificfile)\.(php|html?)$ https://example.com/%2? [R=301,L]
# 3) from https://example.com/specificfile.php?target=dynamicid&baslik=dynamic-slug-url
# to https://example.com/dynamicid/dynamic-slug-url
RewriteCond %{QUERY_STRING} ^target=([^/.&]+)&baslik=([^/.&]+)$
RewriteRule ^(.*)(index|specificfile)\.(php|html?)$ https://example.com/%1/%2? [R=301,L]
Demo on https://htaccess.madewithlove.be

URL Rewrite subdomain with variables to regular domain with variables

I am trying to get some help with this URL rewrite. I have already read multiple tutorials and documentation pages on how to do all this, but none of it makes sense to me. I also don't understand regular expression, so that doesn't help either. I have a semi working piece of code and just need help getting it working correctly.
I need: http://subdomain.domain.com?dl=2
to redirect to http://domain.com/subdomain.php?dl=2
The code I have is
RewriteEngine on
RewriteCond %{HTTP_HOST} ^subdomain.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !page.php
RewriteRule ^(.+/)?([^/]*)$ page.php?dl=$2 [QSA,L,NC]
Which sends the variable but can't figure out the subdomain part. If anyone could please help me out, it would be greatly appreciated.
You need to check the QUERY_STRING as RewriteRule doesn't include it. In addition, your rule is not using the redirect flag R.
RewriteEngine on
# First, check for the subdomain
RewriteCond %{HTTP_HOST} ^subdomain.domain.com$ [NC]
# Then, check the query string - it should match digits (\d+)
RewriteCond %{QUERY_STRING} ^dl=\d+ [NC]
# Check if we are not at subdomain.php
# (This is redundant, but leaving it here in case you really need it)
RewriteCond %{REQUEST_URI} !^/subdomain.php
# If all the above conditions are true, match a root-request
# and redirect to domain.com/subdomain.php with the query string
# Note: You don't need to actually specify the query string
# in the destination URI - Apache will automatically
# hand it over upon redirect (using the R flag).
# The only time this is not the case is when you
# either add the QSD flag, or append the destination
# URI with a question mark.
RewriteRule ^$ http://domain.com/subdomain.php [R=302,L]
The above will redirect http://subdomain.domain.com/?dl=2 to http://domain.com/subdomain.php?dl=2. If you would like to make the redirect permanent and cached by browsers and search engines, change 302 to 301.

Mod rewrite to redirect except on a certain page

Trying to write a rewrite rule in my htaccess so that any request to /en/shop/index.html or /fr/shop/index.html stays on the server, but if the user goes to any other page it redirects to a different server. Here's what I've got so far and it doesn't work.
RewriteRule ^(.*)/shop/(.*) [L]
RewriteRule ^(.*)$ http://www.newwebsite.com/$1 [R=301]
Add a dash to tell the first RewriteRule that you want the matches to be passed through unchanged:
RewriteRule ^.*/shop(/.*)?$ - [L]
I also removed the first set of parentheses since you're not using the results of the match so there's no need to store the matched patterns. I assumed you might need to match /shop without a trailing slash so that's why the (/.*)? section is there.

Force removal of index.php with .htaccess

I'm currently using the following to rewrite http://www.site.com/index.php/test/ to also work directly with http://www.site.com/test/, but I would like to not only allow the second version, I would like to FORCE the second version. If a user goes to http://www.site.com/index.php/test/ it should immediately reroute them to http://www.site.com/test/. index.php should never appear in a url. Stipulation: this should only apply to the first index.php. If I have a title like http://www.site.com/index.php/2011/06/08/remove-index.php-from-urls/ it should leave the second index.php, as it is part of the URL.
Current rule that allows but does not force:
#Remove index.php
RewriteCond $1 !^(index.php|images|css|js|robots.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Thanks.
As you wrote, if a user goes to http://www.site.com/index.php/test/ this rule will imediately reroute him to http://www.site.com/test/
RedirectMatch 301 /index.php/(.*)/$ /$1
I'm not sure if that is what you need as your current rewrite rule is opposite to mine.
First (and wrong) answer - see below
You can accomplish a redirection with these directives (in this order):
RewriteCond %{REQUEST_URI} ^index.php
RewriteRule ^index\.php/(.+)$ /$1 [R,L]
RewriteCond $1 !^(index.php|images|css|js|robots.txt)
RewriteRule ^(.*?)$ /index.php/$1 [L]
That will first redirect all the requests that begin with index.php to the corresponding shortened url, then silently serve index.php/etc with the second rule.
EDIT - Please read on!
In fact, the solution above generates an infinite redirection loop, because Apache takes the following actions (let's say we request /index.php/abc):
first RewriteCond matches
Apache redirects [R], that is, generates a new HTTP request, to /abc
/abc fails first RewriteCond
/abc matches second RewriteCond
Apache does not redirect, but rewrites this URI (so it makes an "hidden" request), to /index.php/abc . We are again at point 1, that's a loop.
Please note...
By using the [L] (last rule) flag, we can only tell Apache not to process more rewrite rules, but only if the current rule matches. Since a new HTTP request is made, there is no information about how may redirection we have been through yet. So, any time one of the two matches, and in any case it generates a new request (=>loop)
Using the [C] (chain rules) flag is kinda pointless because it makes Apache process a rule only if the previous rule matches, while the two rules we have are mutually excluding.
Using the [NS] (not if subrequest) flag on rule #1 is again not an option because it aƬsimply does not apply to our case (see Apache RewriteRule docs about it)
Setting env variables is not an option (alas), since a new request is made at pt 2, thus destroying all environment variables we set.
An alternative solution can be to rewrite e.g. /abc , to /index.php?path=abc. That is done by these rules (please, delete your RedirectMatch similar rule before adding these):
RedirectMatch ^/index\.php(/.*) $1
RewriteCond %{REQUEST_URI} !^/(index.php|images|css|js|robots.txt|favicon.ico)
RewriteRule ^(.+) /index.php?path=$1 [L,QSA]
I don't know the internals of CodeIgniter's scripts, but as most of the MVC scripts, it will read $_REQUEST['PATH_INFO'] to understand which page is requested. You could slightly modify the code that recognizes the page like this (I assumed that the page path is stored in the $page var):
$page = $_REQUEST['PATH_INFO'];
if(isset($_GET['path']) && strlen($_GET['path'])) $page = $_GET['path']; // Add this line
This won't break the previous code and accomplish what you asked for.

Apache / Linux - .htaccess how to get specific variable from a query / url and apply to rewrite

I have a rule that works for one "direction" but, not the other.
A typical incoming url / query would be: (long url)
http://somedomain.com/getme.pl?dothis=display&partnum=1234567 (could be up to 9 digits)
I have this rule in place in my htaccess file:
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L]
Which works great for a bit of ease getting one of the unique part numbers:
http://somedomain.com/1234567.
However, I would like to make the long url "pretty" so, I assumed I could reverse(ish) it.
So, when a link on the site is clicked on (the long url) the htaccess file would process the long url to the beautified version.
I tried MANY attempts.
Here was my latest failure.
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(works)
RewriteCond %{QUERY_STRING} ^partnum=([0-9]*) #(tried to get partnum)
RewriteRule ^.* http://%{HTTP_HOST}/%1 [R] #(make the short url)
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1 [L] #(the known working rule)
I have tried a plethora of rules and visited many sites for advice.
I tried with just rules, just conditions and variations of query_string.
So, I believe I must just grab the "partnum" from the query and rewrite to /1234567 or http_host/1234567
Then, allow the other rule (works) to process.
So BOTH:
http://somedomain.com/getme.pl?dothis=display&partnum=1234567
and
http://somedomain.com/1234567
Display as: http://somedomain.com/1234567 in the browser.
and both passed the whole query to the getme.pl script properly.
I found some close answers here but, none that really explained what I needed.
Can someone please help?
From the sounds of it, this should get you moving down the right path:
# Your working rewrite, with extra param on the rewrite
RewriteRule ^([0-9]*)$ /getme.pl?dothis=display&partnum=$1&rewrite [L]
# Redirect for long urls, to pretty url
# -The appended '&rewrite' on the first rule will force this not to match
# -The trailing '?' on the rewrite will strip the query string
RewriteCond %{QUERY_STRING} partnum=([0-9]*)$
RewriteRule (.*) /%1? [L,R=301]
Hope that helps.

Resources