.htaccess rewrite rule for specific URL - php

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

Related

Friendly Url don't get the link [duplicate]

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

PHP redirect to another page with dynamic url and get data [duplicate]

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

Request param from url in mvc using htaccess and php

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]

Apache Rule not working

I want to create an .htaccess rule.
My link looks like below:
http://gpuzzles.com/brain-questions/funny-puzzles-and-riddles
This can be redirected to gpuzzles.com/brain-questions/index.php (and i can get parameters as funny-puzzles-and-riddles)
I tried to create a new RewriteRule, but it's not working:
RewriteRule ^brain-questions/(.+)$ /brain-questions/index.php?var=$1
Try this code in /brain-questions/.htaccess:
RewriteEngine On
RewriteBase /brain-questions/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?var=$1 [L,QSA]

Mod Rewrite Htaccess for Fake Directories

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]

Categories