I am creating a form at poll.php?code=STRINGHERE
I want to rewrite it with .htaccess to do WEBSITE.com/STRINGHERE
I have done it before but don't remember what I need to do in the .htaccess its something like
RewriteEngine On
RewiteRule ^(\w.+)$ ./poll.php?code=$1
Although I might be wrong. Any help would be great.
Try:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewiteRule ^([^/]+)$ ./poll.php?code=$1 [L,QSA]
And if you need to redirect the browser when requests are made directly to poll.php:
RewriteCond %{THE_REQUEST} \ /+poll\.php\?code=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
Related
I have the php file page.php.
URLS:
page.php?content=contact
page.php?content=aboutus
I need it to be shown like this:
mysite.com/contact
mysite.com/aboutus
I figured out that I have to make .htaccess
RewriteEngine on
RewriteRule ^paginas/([^/]+)/?$ page.php?page=contact [L]
Tryed many things.. No results..
Thanks for attention
Try:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^(?:GET|HEAD)\ /+page\.php\?content=([^&\ ]+)
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /page.php?content=$1 [L]
This will first redirect: /page.php?content=anything to /anything then internally rewrite back requests like /anything to /page.php?content=anything.
Hello i want to convert php urls into SEO urls every thing is working fine at my end but the files in the root doesn't work here is the code which i am using in htaccess
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/]+)$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_staff.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)$ show_users.php?url=$1
RewriteRule ^([a-zA-Z0-9-/]+)/$ show_users.php?url=$1
but if i add these lines right after the above lines the files which are in root doenst work without .php extention here is what i want to add here i want to mention without or with using below code the above code works fine for me
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* $0.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
It looks like your stuff is all in the wrong order. Some things:
If you don't have an L flag for a rewrite, the rules after will continue to process the rewritten request. So you want to use the L flag.
Rewrite conditions only apply to the immediately following rule, so you've got a condition that's just dangling somewhere and not being used.
The show_users.php rules will never work the way you've set things up, the regex pattern is the same, so no matter what, your requests will always go to show_staff.php.
Your rules aren't in the right order, two things need to happen if I'm guessing what you're trying to do. You need to match against a request with a php extension and externally redirect the browser, then you need to internally rewrite that request back to the URI with a php extension.
So something like:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \ /+([^/]+)\.php
RewriteRule ^ /%1 [L,R]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}.php -f
ReweriteRule ^ %{REQUEST_URI}.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9-/]+)/?$ show_staff.php?url=$1 [L]
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
## hide .php extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9/-]+)$ show_staff.php?url=$1 [L,QSA]
I am trying to rewrite the following URL via .htaccess:
http://website.com/dealer_home.php?id=dealer1
The result I am aiming for is this, where dealer1 is the username which is set as variable:
http://website.com/dealer1
I have tried this rule in .htaccess:
RewriteEngine On
RewriteRule ^([^/]*)$ /dealer_home.php?id=$1 [L]
However I get "Internal Server Error" message when trying to load any of the website pages.
Can you provide some advice where I am doing wrong?
EDIT:
I tried also RewriteRule ^(.*)$ dealer_home.php?id=$1 [PT] but no success.
Thank you!
Maybe it's a conflict with existing files/folders and root uri.
Try this code instead
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
This rule will match every url like domain.com/something if something is not an existing file or folder.
So if you have other rules then you should put them above this one.
EDIT: to avoid duplicate content and to redirect old format to new url format
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/dealer_home\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ /dealer_home.php?id=$1 [L]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /domain.com/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*?)/?$ /domain.com/$1.php [L]
am using this code for remove question mark but its not working, trying http://domain.com/details?id=71 to http://domain.com/details/id/71
Please help me where am wrong?
Thanks in advance
I'm not sure what you're trying to accomplish with the first rewrite, but for the second (the last three lines), if the incoming URI is not an actual directory or file, you want to rewrite something like details/id/71 to /details.php?id=71?
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L]
should get you close, assuming there are always 3 fields. In a RewriteRule you don't put the domain name again.
Your code doesn't do anything remotely close to what you want. Your code simply removes the php extension when a request is made explicitly with the extension and adds it back internally. In order to make http://domain.com/details?id=71 get redirected to http://domain.com/details/id/71, you need:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\s/+([^/]+?)(?:\.php|)\?([^=]+)=([^&\ ]+)
RewriteRule ^ /%1/%2/%3? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1?$2=$3 [L]
you want this before your rules that remove the php extension
Assuming your script is called details.php, the following single rule:
RewriteRule details/(.*)/(.*)/$ /details.php?$1=$2
Should rewrite www.domain.com/details/id/71 to www.domain.com/details.php?id=71
You don't need all that other stuff to remove the .php extension.
I have a URL from my website like this -
http://www.mydomain.com/profiles/centers/index.php?code=2507&name=Tharanga+Higher+Educational+Institute#page=art-section
I need to display above url as friendly url on my browser's address bar when it going to above page.
My expecting url something like this -
http://mydomain.com/centers/Tharanga-Higher-Educational-Institute#page=art-section
This code so far in my .htaccess file
# Enable Rewriting
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /profiles/centers/index.php\?code=([0-9]+)&name=([^&]+)&?([^\ ]+)
RewriteRule ^profiles/centers/index\.php /%1/?%2 [R=301,L,NE]
But this code is not working for me. hope someone will help me out.
Thank you.
Replace your code with this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+profiles/centers/index\.php\?code=([^&]+)&name=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([^/]+)/([^/]+)/?$ /profiles/centers/index.php?code=$1&name=$2 [L,NC,QSA,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.(js|css|jpe?g|gif|png)$ /profiles/centers/%{REQUEST_URI} [NC,L,R=302]
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.