fellow programmers,
It's my first time using htaccess for rewrite purposes and i can't figure out a solution for my problem.
I have created a simple rewrite rule to redirect my users to a cleaner url using php GET variables.
Here's the code
RewriteEngine on
RewriteRule ^page/([0-9a-zA-Z]+)/([0-9a-zA-Z]+) index.php?dir=$1&admin=$2 [NC,L]
ErrorDocument 404 /page/404
For now this example doesn't work unless both parameters are set. If i'd like to visit
mysite.com/page/dashboard
then it redirects me to 404 but if add
mysite.com/page/dashboard/random
Then i get my dashboard
I need it to work both ways, if only dir is set and if both are set
Also can i remove that /page/ directory without it messing up my styles, scripts etc? Whenever i remove /page/ and leave it just mysite.com/whatever then my styles and scripts stop working because i guess it's expecting those parameters to be met.
I know it might sound a bit confusing but hopefully someone could help me accomplish this.
So i fixed this issue, seems like the issue was because i didn't check if the url had a slash at the end or not, so i added this.
RewriteEngine on
RewriteRule ^([a-zA-Z0-9]+)$ index.php?dir=$1
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/$ index.php?dir=$1&admin=$2
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?dir=$1&admin=$2
ErrorDocument 404 /page/404
Notice the duplicate entries.
I also added a $ sign at the end so that apache would not except any more redirects or parameters.
Related
So my current URL is: https://example.com/uDoc?id=xx
Using the rewrite, I'd like it to be https://example.com/display?id=xx&type=doc
The real problem is, that because I also have a rule(RewriteRule (.*) $1.php [L]) for silently adding the .php (uDoc?id=xx -> uDoc.php?id=xx, without it, it would result in a 404 error).
Which is somewhat interfering with the current answer given.
I have fiddled around with various htaccess rules, but I got no good results :(
I'd really appreciate it if someone could help me, when it comes to htaccess rules, I'm totally giving up...
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^uDoc/?$ display?type=doc [L,QSA,NC]
QSA flag ensures your existing query string i.e. id=xx is carried over to new URI.
I am new to url rewriting, and saw many tutorials for url rewriting like,
url-rewriting-for-beginners
I have used the following code in .htaccess,
RewriteEngine On
RewriteRule ^/raipur/([0-9])$ /viewRestaurant.php?id=$1 [NC,L]
and testing on this page urlRewritetest.php.
code of urlRewritetest.php
click
but its not working, I am using ipage's server, and also when i try to see the log using RewriteLog, the page through Internal Server Error,
Can anyone please suggest my mistake, thank you
What if you remove the first slash in your RewriteRule? You may also want to add "+" after the digits to match more than one digit.
RewriteEngine On
RewriteRule ^raipur/([0-9]+)$ /viewRestaurant.php?id=$1 [NC,L]
so I have this htaccess entry:
RedirectMatch /([a-zA-Z0-9]+).php /dirA/$1.php
The goal is that any .php that is on the root directory should be redirected to /dirA/*.php
eg. suppose I make the request
domain.com/something.php
it should instead redirect to
domain.com/dirA/something.php
However when I put that entry in my .htaccess file and then I go to domain.com/something.php
it instead returns
"The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for
this address in a way that will never complete."
Any idea how I can modify my htaccess to accomplish what I want to do?
Updated Question
Also is there a way to make it so that it only redirects if the file doesn't exist in the root directory...hence if x.php exists in root, serve that x.php otherwise redirect to dirA/x.php
mod_rewrite is an overkill for this, you were on the right track with RedirectMatch. Your rule, however, is a bit faulty: the regex /([a-zA-Z0-9]+).php matches all string that contain the specified substring, so it matches "/foo/bar/baz.php", but also "dirA/foo/bar.php" (and even "/foo/bar.php/baz.php"I. Your redirection ended up in an endless loop because there was no stop condition: /dirA/foo.php was redirected to /dirA/foo.php.
You can remedy the situation by using anchors in the regex:
RedirectMatch ^/([a-zA-Z0-9]+).php$ /dirA/$1.php
As for your second question: that might indeed call for mod_rewrite. Something along these lines should do the trick:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+).php$ /dirA/$1.php [R=301]
I haven't tested it, but this should get you started. Make sure to check out the manual for details, or just search around on SO, there are tons of questions about this.
Try this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+).php /dirA/$1.php [R=301,L]
This one should be just fine
RewriteRule ^(.*)$ /subdir/$1
I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.
I have a multilanguage site and I'm trying to rewrite the URL's with a fake directory something like this:
http://localhost/theSite/page.php?id=param&cat=param?lang=en,fr,es
to http://localhost/theSite/(en|fr|es)/page/param/param
.htaccess
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(fr|en|en)/(.*) $2.php?id=$1&cat=$2&lang=$3 [NL,QSA]
This resolves as a 404 error.
Any help will be apreciate.
RewriteRule ^(en|fr|es)/(.*?)/(.*?)/(.*) $2.php?id=$3&cat=$4&lang=$1 [NC,QSA]
I suppose you meant NC (no case), not NL. You referred to capture groups that didn't exist and repeated $2.
You're second capture will capture everything until the end of the URL. So it is possible you are doubling up on the extension or the wrong directory.
Although it shouldn't affect the redirect, you don't have a third capture, so where is $3?
Look at your headers and see where it is really redirecting to and comment back.