Where's the error in my .htaccess RewriteRule Regexp? - php

I want this url
http://www.test.racebooking.net/rankings/classifica.html
to point to
http://www.test.racebooking.net/rankings.php
thus, i wrote this rule in my .htaccess file
RewriteRule ^rankings\/[a-zA-Z0-9\-]+\.html$ /rankings.php
I really can't figure out why, when i type the first url, i get a 404 error msg.
RewriteEngine is on.
/rankings.php exists.
Moreover, I have another RewriteRule, much more complex, which i
wrote, which is working like charm.
classifica.html doesn't exist. I'm just updating my website with
more SEO friendly urls
What's wrong with my rule??
When i test the regexp in any online regexp tester it detects the string "rankings/classifica.html", this means the regexp actually corresponds, but nothing is working....

I found the error, but i didn't find out why it happens.
Basically, if pattern and substitution start with the same word, the RewriteRule doesn't work.
EXAMPLE
If i write this rule in .htaccess:
RewriteRule ^rankings\/[a-zA-Z0-9\-]+\.html$ /rankings.php
and type this URL
http://www.mywebsite.net/rankings/classifica.html
I get a 404 Error. Even if the Regexp is correct, the apache RewriteEngine is not working. Instead, if i write this rule:
RewriteRule ^ran-kings\/[a-zA-Z0-9\-]+\.html$ /rankings.php
and type this URL
http://www.mywebsite.net/ran-kings/classifica.html
Everything works fine.

Related

Setting a rewrite rule when condition is met

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.

Htaccess rule for a rewrite with parameters

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.

Rewrite rule is not working in apache

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]

php htaccess url rewrite issue

Im trying to get a url rewrite to work and Im not getting any where. basically I want to have three pages one for music and then general products and the other for mvies, each will have a query string of &pagetype=general etc but the url would be http://shopnbuy.vacau.com/browse/computers.html?pageType=general which will redirect to general_products.php and then there would music_products and also movie_products. So they all have the same url except there is a different parameter &pageType=. Below is my htaccess file this is my first attempt at doing this
# Do not remove this line, otherwise mod_rewrite rules will stop working
RewriteBase /
RewriteEngine On
RewriteRule ^browse/([a-zA-Z0-9_-])\.html general_products.php?department=$1&pageType=general
RewriteRule ^browse/([a-zA-Z0-9_-])\.html music_products.php?department=$1&pageType=music
RewriteRule ^browse/([a-zA-Z0-9_-])\.html movie_products.php?department=$1&pageType=movie
url suppose to look like this
http://shopnbuy.vacau.com/browse/computers.html
All Im getting is an error for each url rewrite
I don't know much htacess, but I used it lately and I found out that you can't actually use "/" effectively. I suggest you change it for "_".
As far as I understand, it may be due the "/" works as the one indicating each argument (I mean A / B sorta logic)
Not sure if I explain myself correctly, hoping someone knowledgeable corrects me since I'm eager to learn about it as well.
RewriteRule ^browse_computers / general_products.php?department=$1&pageType=general ([a-zA-Z0-9_-])
RewriteRule ^browse_music / music_products.php?department=$1&pageType=music ([a-zA-Z0-9_-])
RewriteRule ^browse_movies / movie_products.php?department=$1&pageType=movie ([a-zA-Z0-9_-])
The result, however, will be this:
http://shopnbuy.vacau.com/browse_computers
Bit unsure of your initial url but try this
^browse_(.+)/(.+)$ browse/computers.html?pageType=$1

.htaccess RewriteRule for Flat Links

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]+)/?$.

Categories