RewriteRule and passed variables not working - apache

I am trying to make my site more SEO friendly. I researched this and couple other sites and besically none of the solutions work. The URL rewrite works fine with this simple code:
RewriteRule ^testpage\.php$ destination.php [NC,L]
However, when I try to implement RewriteRule on pages that are dynamic and passing variable in URL, nothing happens. This is the code I have right now that does not work:
RewriteRule ^([^/]*)\.html$ /prodinfo.php?prodid=$1 [NC,L]
I would like to have http://www.mysite.com/products/prodinfo.php?id=268
converted to http://www.mysite.com/268.html
I am on Apache 2.2 with php.5.3 Thanks.

Is what you're looking for something like this?
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products/prodinfo\.php\?id=([0-9]+)
RewriteRule ^ /%1.html? [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+).html$ /products/prodinfo.php?id=$1 [L]

Anyone who accesses: http://example.com/268.html in their browser has it converted server-side into: http://example.com/products/prodinfo.php?id=268 with this:
RewriteEngine On
#RewriteRule ^([0-9]+).html/?$ /products/prodinfo.php?id=$1 [NC]
... or to do the opposite and translate a visitor's request for: http://example.com/products/prodinfo.php?id=268 to http://example.com/268.html in their browser use:
RewriteEngine On
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^products/prodinfo.php$ /%1.html? [R]

Related

URL Rewriting Using .htaccess for PHP

I want to convert this URL example.com/post.php?id=12345&title=xyz to example.com/12345/xyz.
I am able to do example.com/12345 but I can't get /xyz.
RewriteEngine On
RewriteRule ^post/([a-zA-Z0-9-/]+)$ post.php?id=$1
RewriteRule ^post/([a-zA-Z-0-9-]+)/ post.php?id=$1
With your shown samples, please try following htaccess Rules. Please make sure to clear your browser cache before testing your URLs.
This solution assumes that you are hitting example.com/post.php?id=12345&title=xyz sample url in browser and want to change it to example.com/12345/xyz
##Enabling engine here.
RewriteEngine ON
##Extrenal redirect 301 to mentioned url by OP in question.
RewriteCond %{THE_REQUEST} \s/post\.php\?id=([^&]*)&title=(\S+)\s [NC]
RewriteRule ^ %1/%2? [R=301,L]
##Internal rewrite to post.php with query strings.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/?$ post.php?id=$1&title=$2 [QSA,L]

beautify url from art.php?id=51 to art/51

want to beautify an url from:
https://abuena.net/art.php?id=51
to
https://abuena.net/art/51
here is my try, without success, simply is ignored
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_]+)$ /$1/ [R]
RewriteRule ^([a-zA-Z0-9_]+)/$ /art.php?id=$1
My hosting provider says that my server is not Apache but LSWS - LiteSpeed WebServer - and that is probably problem in my syntax.
Any help?
Could you please try following. Considering that you need to hit URLs like https://abuena.net/art/51 in your browser. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1.php?id=$2
EDIT: In case you are hitting URL like https://abuena.net/art.php?id=51 in your browser then try following once.
RewriteEngine ON
RewriteCond %{REQUEST_URI} \s/([^.]*)\.php\?id=(\d+)\s [NC]
RewriteRule ^ http://%{HTTP_HOST}/%1/%2 [R=301]
RewriteRule ^([^/]*)/(.*)/?$ $1.php?id=$2 [L]

httaccess change mydomain.com/knowledgebase.php to mydomain.com

I checked many other similar questions but I can't come up with a .htaccess rule that would work properly.
I have a server containing a Knowledgebase system (knowledgebase.php). I don't want to show this knowledgebase.php in the URL, ever.
Examples what I want:
https://help.thecrypto.app should show same url (now it shows https://help.thecrypto.app/knowledgebase.php when you visit)
https://help.thecrypto.app/knowledgebase.php?article=1 should show URL https://help.thecrypto.app/?article=1
How can I do this?
I tried many options, including this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /knowledgebase.php?/$1 [L]
Could you please try following, written based on your shown samples. Please clear your browser cache before testing urls.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/?$
RewritewriteRule ^ knowledgebase.php [L]
RewriteCond %{REQUEST_URI} ^/(knowledgebase) [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/?article=1 [L]
You can use this :
RewriteEngine on
#1) redirect "help.thecrypto.app/" to /knowledgebase.php
RewriteCond %{HTTP_HOST} ^help.thecrypto.app$ [NC]
RewriteRule ^knowledgebase\.php$ / [L,R]
#2) internally map knowledgebase.php to the root /
RewriteRule ^/?$ /knowledgebase.php [END]
This will serve /knowledgebase.php if you visit your site hompage / .

Wildcard in Modrewrite htaccess APACHE redirection

In my .htaccess APACHE's file I have the following code:
#MY AWESOME WEBSITE
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^myawesomewebsite.com
RewriteRule ^(.*) http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website [P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^www.myawesomewebsite.com
RewriteRule ^(.*) http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website [P]
Both, myawesomewebsite.com and coolwebsite.com are in my hosting linked properly, myawesomewebsite.com is an alias.
That code works fine. But IF I go to:
myawesomewebsite.com/index.php
myawesomewebsite.com/wtf.php
myawesomewebsite.com/blahblahblah
myawesomewebsite.com/whateveryouwrite
You will see the content of coolwebsite.com/index.php in all cases.
I want redir: .*myawesomewebsite.com.* to http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website
How can I achieve this?
Thanks for reading, and please forgive my bad english.
You are using the [P] flag, which is for proxying and not what you want. Replace all your rules with only this:
RewriteCond %{HTTP_HOST} ^(?:www\.)?myawesomewebsite\.com$
RewriteRule ^ http://coolwebsite.com/web/this-is-a-friendly-url-version-of-my-awesome-website [R=301,L]
I took out the condition to only redirect if the file doesn't exist, since you didn't say you wanted it to work that way.
Let me know if I misunderstood what you're trying to acheive.
Update
Try this instead:
RewriteCond %{HTTP_HOST} (?:^|\.)myawesomewebsite\.com$
RewriteCond %{REQUEST_URI} !=/web/this-is-a-friendly-url-version-of-my-awesome-website
RewriteCond %{REQUEST_URI} !=/web.php
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule ^ /web/this-is-a-friendly-url-version-of-my-awesome-website [L]

htaccess SEF URL rewriting not working with question mark

Please I'm facing some challenges with my URL rewriting.
I'm trying to rewrite:
domian.com?v=subj to domian.com/subj
and
domian.com?v=subj&id=3 to domian.com/subj/id
So users can directly access a clean URL.
Here's what I tried that wasn't working:
RewriteCond %{QUERY_STRING} ^v=(.*)$
RewriteRule ^index.php/?$ $1? [NC]
I also tried this too:
RewriteRule ^(.*)/([0-9])$ http://domain.com/$1/$2 [NC]
Both were not working. Please I need some clarification on this. Thanks.
You can use the following 2 rules to redirect query strings to SEF format.
RewriteEngine on
#--Redirect "?v=foo&id=bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php\?v=([^&]+)&id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]
#--Rewrite "/foo/bar" to "?v=foo&id=bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?v=$1&id=$2 [NC,QSA,L]