I am trying to create fake directories to redirect specific parameters to specific subdirectories as such:
From: www.example.com/username
To: www.example.com/posts?uid=alex
From: www.example.com/p/1234
To: www.example.com/posts?url=1234
It is somehow more complicated example from the rest questions with the same subject on stackoverflow and most answers do not give an explanation on how it works, thus I am not able to come into a conclusion on how I could solve this.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^p/([^/]+)$ /posts?url=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /posts?uid=$1 [L]
First, you need to make all of your links look like this:
www.example.com/username
www.example.com/p/1234
The rewrite rules above will match the /p/1234 (via ^p/([^/]+)$) and /username (via ^([^/]+)$) and internally rewrite them to the /posts URI. Note that this will not change the URL in the browser's location bar, as that is an external redirect. If someone enters www.example.com/posts?uid=alex, the URL will be unchanged.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^p/([^/]+)/?$ /posts?url=$1 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /posts?uid=$1 [L,QSA,NC]
Related
Today i start with my mod rewrite for the php script.
Some words to the setup
I use a subdomain e.g. test.testsystem.com which points to root/test/
So i have some multiple GET Parameters for my PHP script like the following:
1) https://test.testsystem.com/index.php?a=foo
2) https://test.testsystem.com/index.php?a=foo&v=bar
3) https://test.testsystem.com/index.php?a=foo&c=testcode
so with mod rewrite i want to have
1) https://test.testsystem.com/foo
2) https://test.testsystem.com/foo/bar
3) https://test.testsystem.com/foo/c/testcode
What i currently have is
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /test/
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1 [QSA]
RewriteRule ^([a-z]{2,2})/([a-zA-Z0-9_-]+)$ index.php?a=$1&v=$2 [QSA]
For the first two URLs. Now it does no error 500 any more but the problem is i do not get any GET parameters in my php script.
Maybe someone can provide me here a working solution.
Regards
Assuming your test.domain.com is pointing to the /test folder , you can use something like the following in your /test/.htaccess .
Options -Multiviews
RewriteEngine on
#1)Rewrite /foo to /index.php?a=foo
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?a=$1 [L]
#2)rewrite /foo/bar to /index.php?a=foo&v=bar
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2 [L]
#3)rewrite /foo/bar/buzz to /index.php?a=foo&v=bar&z=buzz
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?a=$1&v=$2&z=$3 [L]
The RewriteConds are important in each rules to avoid rewriting your existing files and directories .
I have the following rule:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)(?:/([^/]+))?/?$ index.php?a=$1&b=$2 [QSA]
This rule redirect all requests to my index.php and works like a charm. Now I would like to ADD another rule that redirect all requests for the directory /admin/ to admin.php in root. (A request for www.mysite.de/admin/login should end in www.mysite.de/admin.php?a=login&b=) The second parameter b should be optional.
How to reach this? Thx for any hint in advance.
You can test htaccess rules online (ex. here: htaccess.madewithlove.be)
This code works with:
www.mysite.de/admin/login
and
www.mysite.de/admin/login/123
edited
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^admin/?([^/]*)/?(.*)$ admin.php?a=$1&b=$2 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/?(.*)$ index.php?a=$1&b=$2 [NC,QSA,L]
#removed
#RewriteRule ^admin/([^/]+)(?:/([^/]+))?/?$ admin.php?a=$1&b=$2 [NC,QSA,L]
#RewriteRule ^([^/]+)(?:/([^/]+))?/?$ index.php?a=$1&b=$2 [QSA]
Notice that you need the L flag there. (RewriteRule Flags)
Also useful to see How L flags work - StackOverflow answer and Everything about mod rewrite - serverfault.com thread
I am just new to .htaccess.
I need some rewrite rules for URLs.
I Google'd some and applied but no change in URL.
I want:
demo.example.com/section.php?id=1
Changed to:
demo.example.com/section/sample-section
i tried
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^section/(\d+)*$ ./section.php?id=$1
but no difference
Thanks.
I will appreciate your help.
First, make sure mod_rewrite is enabled and htaccess files allowed in your Apache configuration.
Then, put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
# redirect "/section.php?id=xxx" to "/section/xxx"
RewriteCond %{THE_REQUEST} \s/section\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ /section/%1? [R=301,L]
# internally rewrite "/section/xxx" to "/section.php?id=xxx"
RewriteRule ^section/([0-9]+)$ /section.php?id=$1 [L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^section/([^/]+)$ /section.php?id=$1 [L]
This will turn example.com/section.php?id=X to example.com/section/X
I suggest storing the URI in a database then using section.php?uri=
For example:
example.com/section.php?uri=super-awesome
would turn into:
example.com/section/super-awesome
When user hit a URL like this
www.mydomain.com/premium-pro/somepage.html
It should map to the
www.mydomain.com/myfile.php
and I should be able to access somepage.html as a $_GET parameter in myfile.php
I have .htaccess under mydomain.com but very new to rewrite rules and not sure what kind of rule should be written there. I did research on this but didn't get succeed.
Please help
Try with below rule in root, I've taken a var variable for $_GET data.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/premium-pro/(.+)
RewriteRule ^ myfile.php?var=%1 [L]
Try this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule myfile.php ./premium-pro/somepage.html
The real url format is
http://localhost/myfolder/index.php?url=login/register&step=1
After mod rewrite I get
http://localhost/myfolder/login/register/step/1
All good but how can I get the value of step in a php file.If I do $_GET['step'] then I get nothing.
My htaccess
Options -MultiViews
# turn rewriting on
RewriteEngine On
# When using the script within a sub-folder, put this path here, like /mysubfolder/
# If your app is in the root of your web folder, then please delete this line or comment it out
RewriteBase /mybb/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)/(.*) index.php?url=$0&step=$2 [L,QSA]
Change your rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)(?:/(step)/([0-9]+))?/?$ index.php?url=$1&$2=$3 [NC,L,QSA]