htaccess SEF URL rewriting not working with question mark - apache

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]

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

.htaccess - Redirect domain/lang/ to subdomain for each language

I've been struggling with this for weeks and I can't get to the solution, I have to make a redirect in .htaccess of a multi-language website with all its pages following the same structure but with a subdomain for each language. In the following way that is valid for http as https:
domain.es/es/allpages to es.subdomain.com/allpages
domain.es/pt/allpages to pt.subdomain.com/allpages
domain.es/de/allpages to de.subdomain.com/allpages
domain.es/fr/allpages to fr.subdomain.com/allpages
domain.es/en/allpages to eu.subdomain.com/allpages
Any ideas?
I have tried the following but not sure if it is correct:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) https://es.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/pt/(.*)$
RewriteRule ^(.*) https://pt.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/fr/(.*)$
RewriteRule ^(.*) https://fr.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/de/(.*)$
RewriteRule ^(.*) https://de.subdomain.com/%1 [R=301,NC]
RewriteCond %{REQUEST_URI} ^/en/(.*)$
RewriteRule ^(.*) https://eu.subdomain.com/%1 [R=301,NC]
Thanks a lot!!!!
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before you test your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.es$ [NC]
RewriteRule ^(es|pt|de|fr|en)/(.*)/?$ http://$1.subdomain.com/$2 [NE,R=301,NC,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]

Need help removing special characters rewrite htaccess

Struggling with a 301 redirect in htaccess after changing my links on my blog.
I have the following code in htaccess today:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^.*$ http://example.com%{REQUEST_URI} [R=301,L]
RewriteRule ^sitemap.xml$ sitemap.php [L]
Rewriterule ^blog/(.+)/(.+).html$ ./blog/view/blog.php?page=$1&mode=$2 [NC]
Rewriterule ^blog/(.+).html$ ./blog/blog.php?page=$1 [NC]
Rewriterule ^blog/(.+)/$ ./blog/view/blog.php?page=$1 [NC]
Rewriterule ^blog/$ ./blog/blog.php [NC]
RewriteCond %{REQUEST_FILENAME} !-f
Rewriterule ^blog/(.+)/(.+)$ ./blog/view/blog.php?page=$1&mode=$2 [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/(.+)$ ./blog/blog.php?page=$1 [NC]
The problem is that I´ve now in php converted special characters like 'åäö' to 'aao' but the old links to my old posts has no redirect to the new links.
Can someone please help me out here.
Here is an example of a simple 301 Redirect that I based the following code from.
Here is an example of redirection based on extension to fit your requirement.
RewriteEngine On
RewriteCond %{REQUEST_URI} .html$
RewriteRule ^blog/(.*).html$ /blog/blog.php?page=$1[R=301,L]