Htaccess rule for a rewrite with parameters - php

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.

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.

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]

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

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.

PHP Mod_rewrite

i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.

.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