Mod_rewrite rule not passing PHP variables - php

I am trying to rewrite the URL (using mod_rewrite) as requested by the user:
http://example.com/question/1/
To load this URL from the server:
http://www.example.com/question.php?qid=1
I am currently using the rewrite rule:
RewriteEngine On
RewriteRule ^question/([^/\.]+)/?$ question.php?qid=$1 [L]
But this does not work. I have tried other things in the .htaccess (such as redirects) and this work, so I know that the .htaccess is being processed.
Thank you.

According to your example, your rewrite rule should be something like:
RewriteRule ^question/(\d+)/?$ /question.php?qid=$1 [L]

Related

Rewrite .htaccess resulting in 404

I'm trying to make my url look good but for some reasons it is not working what could be the problem here
RewriteEngine on
RewriteRule ^view/([^/]*)/([^/]*)$ /view?wall=$1&page=$2 [L]
Above rewrite should resolve like this http://website.com/view/apartment-wallpapers/1247
Alternatively
Preferably I want to make my url to look like this but it's also resulting in
http://lifistudy.com/asus-red-903.html
.htaccess rewrite rule
RewriteEngine on
RewriteRule ^([^-]*)-([^-]*)\.html$ /view?wall=$1&page=$2 [L]
Note: using openlitespeed and all .htaccess rules are working
The issue is in your rewrite rule. You're using ([^/]*) which is incorrect syntax use (.*) and your rewrite rule will work perfectly.

Apache mod rewrite rule is not working

I already checked and the MOD REWRITE is up and running on the server.
Request url is:
http ://www.mydomain.com/user/123456
.htaccess rule:
RewriteRule ^/user/(.*)$ /user.php?user=$1 [L,QSA]
So in theory it should redirect internally to the php script user.phpwith the variable u=123456 but nothing is happening? How can I debug that?
Not Found
The requested URL /user/65464654 was not found on this server.
You need to get rid of the leading slash in your pattern. The leading slash is removed from the URI when used to match against rules in an htaccess file. So you want:
Options -Multiviews
RewriteEngine on
RewriteRule ^user/(.*)$ /user.php?user=$1 [L,QSA]
The -Multiviews option may not be needed, it's to ensure mod_negotiation doen't take over and automatically map the request to user.php before your rewrite rule can get applied.
if .htaccess is in /user then RewriteRule ^\d+$ user.php?user=$0 [L,QSA]
from #Deadooshka works.

htaccess redirect from asp to php with variables

How can I redirect the pages ASP to the new PHPpages ?
Example:
from /pages/fauna/fauna.asp?IdF=1
redirect to /pages/fauna/fauna?id=1
I tried with
RewriteEngine On
RewriteCond %{QUERY_STRING} ^IdF=1$
RewriteRule ^pages/fauna/fauna\.asp$ /pages/fauna/fauna?id=1 [R=301,L]
But It doesn't work
Put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{QUERY_STRING} ^IdF=([^&]+) [NC]
RewriteRule ^pages/fauna/fauna\.asp$ /pages/fauna/fauna?id=%1 [R=301,L]
Make sure this is first rule and there is no other .htaccess in your system.
If you're on IIS - which you must be if you're using Classic ASP - then .htaccess doesn't do anything, (unless you have a third party ISAPI rewrite filter installed)
If you're using IIS7 or IIS8 you can add the rewrite rule in your web.config file
http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module
Dont get crazy, just redirect all the .asp request to a php script, and create a router in php, and then handle all the parameters and do what you need in a much more programmer-friendly way.
Take a look at this answer

Rewrite URL using htaccess php

I want to rewrite the url in my project.
For example:
http://www.example.com/dashboard/test/ to http://dashboard.example.com/index.php
Also I want to do it for:
http://www.example.com/dashboard/test2/ to http://dashboard.example.com/index.php
Can anyone tell me the idea to rewrite the url?
First you create a .htaccess file in your root.
Than, just put the redirect commands in it.
How to compose a .htaccess and how to create rewrite rules is explained here: http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/
You will need something like:
RewriteRule ^(.*)$ http://dashboard.example.com/$1 [L,QSA]
try this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^test.*$ http://dashboard.example.com/index.php [R=301,L]

Redirect subdomain to CakePHP action

Background
I have a CakePHP application that lives in /m/. I want to write a root-level .htaccess file which will redirect "subdomains" for the site as parameters to actions.
For example: I want to write a rewrite rule which will result in redirects like this -
http://mysite.myserver.com → http://myserver.com/m/mysite/
http://mysite.myserver.com/home → http://myserver.com/m/mysite/home
http://mysite.myserver.com/foo/bar?baz=true → http://myserver.com/m/mysite/foo/bar?baz=true
Ideally, this redirect should be invisible to users (I don't want to use a 301, because I don't want to change the URL).
Here's my current attempt:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.myserver\.com$ [NC]
RewriteRule ^(.*)$ http://myserver.com/m/%1/$1 [L]
</IfModule>
As far as I can tell, the main issue is with $_SERVER['REQUEST_URI']:
If I browse to http://myserver.com/m/mysite/home directly, $_SERVER['REQUEST_URI'] = /m/mysite/home.
If I browse to http://mysite.myserver.com/home using the .htaccess file above, $_SERVER['REQUEST_URI'] = /home.
The Issue
Because of the issues with $_SERVER['REQUEST_URI'], my routes aren't parsing correctly: it's trying to take the user to /home rather than /m/mysite/home as desired.
How can I change my rewrite rule to make this work properly? Is there another way to accomplish this?
What you're asking (change the domain name in URL but don't let browser see URL change) is not achievable under normal scenario. However to make it possible you have enable mod_proxy in your Apache and restart it. Once that is done use following code in your site root .htaccess:
RewriteCond %{HTTP_HOST} ^([^.]+)\.myserver\.com$ [NC]
RewriteRule !^m/ http://myserver.com/m/sites/%1%{REQUEST_URI} [NC,L,P]

Categories