.htaccess - Make /some-address the same as /page.php?id=1 - php

I am currently working on a project where I have to redirect a user after a form has been submitted.
Once the form is submitted I redirect the user to a url based on what they entered into the form. An example of what the URL might look like:
/oslo-norway-copenhagen-denmark
I would like /oslo-norway-copenhagen-denmark to display in the users URL bar, would would like it to represent the page /page.php?id=oslo-norway-copenhagen-denmark.
I've currently got this working:
Options +FollowSymLinks
RewriteEngine On
DirectoryIndex search.php
RewriteBase /services
RewriteRule ^formmail/([^/]*)$ formmail.php?id=$1 [L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
The only problem is that this makes /formmail/oslo-norway-copenhagen-denmark work, but how do I go about making this only /oslo-norway-copenhagen-denmark? I tried removing the /formmail/ but this didn't work
Could anyone help me solve this problem? Thank you very much!

All you need to use is this in your .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)$ /services/page.php?id=$1 [L]
Make sure you clear your cache before testing it. It will leave you with your desired URL.

Related

URL friendly PHP form not submitting in Method POST

I am trying to post form in core php and custom .htaccess file. When I am posting my form, it redirects to the posted page but didn't act with the php functions.
All other pages are working well dynamically, only form submission has an issue.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.werepairuae.com
RewriteRule (.*) http://werepairuae.com/$1 [R=301,L]
#form submit------------------
#RewriteRule ^([a-zA-Z0-9-/]+).php$ prob-page-dtls_post.php [QSA,L]
#RewriteRule ^([a-zA-Z0-9-/]+).php/$ prob-page-dtls_post.php [QSA,L]
RewriteRule ^([a-zA-Z0-9-/]+).php/$ prob-page-dtls_post.php [QSA,L]
#-----------------------------
Here is my code sample, how can I fix this issue?
You need to do the redirect differently for POST requests. Try:
RewriteRule ^([a-zA-Z0-9-/]+).php/$ prob-page-dtls_post.php [NC,P]
"Use of the [P] flag causes the request to be handled by mod_proxy, and handled via a proxy request."
https://httpd.apache.org/docs/current/rewrite/flags.html#flag_p
Checkout these other questions for relevant discussion with redirecting POST requests:
Is it possible to redirect post data?
Redirect POST htaccess
Thank to all of you, specially who answer me, after searching on his answer i get this one to add in my .htaccess file and i found my result. And that is i have successfully submit my form and email.
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.werepairuae.com
RewriteRule (.*) http://werepairuae.com/$1 [R=301,L]
RewriteRule ^([a-zA-Z0-9-/]+).php/$ prob-page-dtls_post.php [QSA,L]
i Just added these 2 lines on my file
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
and it works.
Thanks to every one!

Simple htaccess rule for rewriting the URL

Im trying to achieve this
http://domain.com?q=page-slug to http://domain.com/page-slug
right now if I access this http://domain.com?q=page-slug my webpage is working, but I want clean URls like http://domain.com/page-slug
I used this code in my htaccess but returning 500 error
RewriteEngine On
RewriteRule ^(.*)$ ?q=$1 [L,QSA]
any help please.
Try this I am assuming you have index.php page which is handling the request.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)$ index.php?q=$1 [QSA,L]
If not index.php change to actual page you have.

Php url rewrite and redirect in htacess

I want to redirect my site to friendly.
I have my website pull data from database and adress is /post?id=1
and i want to change it to /post/1
I already wrote the code for rewrite but i cant make sense from google research how to redirect to /post/1.
my code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L]
Use:
Options +FollowSymLinks
RewriteEngine on
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /post/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^post/([^/.]+)/?$ post.php?id=$1 [L,QSA,NC]
This one worked for me
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ post.php?id=$1 [NC]
if you goto url /post/check1/
the htaccess file internally call post?id=check1

Vanity URL and redirection

I am trying to make a redirection service, which can redirect users to their profile page, based on a vanity url.
The vanity URL will be of the format - www.site.com/username
This will redirect them to a page profile_redirect.php, which will echo the username (The functionality is deeper, but this is all I need help with).
I tried several of the answers posted here, but I am unable to get this to work. At the moment, it does nothing and just gives me a page not found error. I am running this on wamp, and I have localhost's alias set to "proj", so I call "http://proj/username", but this gives me page not found.
My current htaccess file (Relevant portion)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC]
RewriteCond %{REQUEST_FILENAME} >""
RewriteRule ^([^\.]+)$ profile_redirect.php?user=$1 [L]
My current profile_redirect.php file
$getName=$_GET["user"];
echo $getName;
Have your rules like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)/?$ $1.php [L]
RewriteRule ^([^\.]+)/?$ profile_redirect.php?user=$1 [L,QSA]

Trying to create tiny urls, getting redirect loop

I'm trying to create tiny urls like this:
site.com/abc123
goes to:
site.com/index.php?token=abc123
but I keep getting redirect loops no matter what I try, or it tries to redirect to index.php?token=index.php..
Current .htaccess is:
Options +FollowSymLinks
Options -MultiViews
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?token=$1 [L]
Here's what I've done (I'm redirecting alphanumeric codes like http://myurl.com/b32ad ):
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule (.*) /index.php?token=$1 [L]
I have answered a similar question yesterday: htaccess: Redirect a Dynamic URL - Show only Static URL - Double Content
This should do it:
RewriteCond %{QUERY_STRING} ^token=([a-zA-Z0-9]+)$
RewriteRule ^/ /%1? [R=302,L]
That's strange since you have the [L] option attached to that rule. Could there be an external redirect caused by something else?
Anyway, you could limit the rule to requests for non-existing files (maybe directories, too).
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?token=$1 [L]
see http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond

Categories