.htaccess - How to rename GET variable in URL - .htaccess

I have to rename a GET variable in an URL from:
https://example.com/dev/?id[dokid]=5
to
https://example.com/dev/?doc[dokid]=5
There are many examples for rewriting the parameter, but not for the name.
I' m a .htaccess newbie and need some advice...
Thx fxp

I put this code into apache virtual host config:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} "/dev/" [NC]
RewriteCond %{QUERY_STRING} id\[dokid\]=([-a-zA-Z0-9-&-=_+]+)
RewriteRule ^/dev/$ /dev/?doc\[dokid\]\=%1 [R] [END]
This solution works perfect.

Related

RewriteRule for htaccess note working

I'm trying to get pages that call a variable (from my OLD site) to point to the root level of my blog... using my htaccess file. My knowledge of the htaccess syntax, isn't very good...
For example, any of these pages...
www.example.com/blog/?paged=100
www.example.com/blog/?paged=84
www.example.com/blog/?paged=3
... will ALL be rewritten as ...
www.example.com/blog/
I tried...
RewriteEngine on
RewriteRule ^blog/paged=?$ $1/blog/$2 [R=301,L]
... but that only works for paged=1. Any other variable didn't work (i.e. 2, 100). Can someone help with the correct way to do this?
To be able to match query string you need to use RewriteCond like this.
If /blog/.htaccess doesn't exist:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^blog/?$ /blog/? [R=302,NC,L]
If /blog/.htaccess exists:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^paged=\d+
RewriteRule ^/?$ /blog/? [R=302,NC,L]

How to redirect url to another with the hash in the middle of the address

I would like to redirect that url:
mydomain.com/folder1/folder2/
to:
mydomain.com/#/folder1/folder2
The thing is, that the /folder1/folder2/ part of URL I want to read as a parameters so that is why I am using hash for it.
So it should be something like:
hostdomain.some/different text/another one
to:
hostdomain.some/#/different text/another one
so I can get the different text/another one as a parameters.
I've tried most of the solution given here, but neither helped.
[EDIT] Ok, I think I found a resolution to this.
Solution!
You can use this rule in you root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+/.+)$ /#$1 [L,NE,R=302]
Change 302 to 301 once it is working.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/?$ /#$1/$2 [L,R=301]

What's wrong with my RewriteRule? It seems ok, but it doesn't work

I have a rewrite rule on my webpage.
RewriteEngine On
RewriteRule ^(.*) index.php?p=$1 [L]
I want it to work so it rewrites URLs like this:
http://example.com -> index.php
http://example.com/home -> index.php?p=home
http://example.com/lol -> index.php?p=lol
But when I use the following php code inside my index.php
print_r($_GET)
It gives this:
Array ( [p] => index.php )
It gives this same results in all URLs (I tried these: http://example.com, http://example.com/, http://example.com/about, http://example.com/about/
Can you help me debig this?
I got it figured out:
The correct code is this:
RewriteEngine On
RewriteRule ^([^.]+)/?$ index.php?p=$1 [NC,L]
Sorry for the question.
The issue is that your rewritten URL still matches the rule and you get a new rewrite:
http://example.com/home
http://example.com/index.php?p=home
http://example.com/index.php?p=index.php
Since the [QSA] flag is not set, the new p parameter replaces the previous one. (I'm not fully sure about why you don't get an infinite loop, I suppose mod_rewrite makes a check to avoid useless redirects).
You need to add additional conditions. For instance, you can make a rewrite only if the URL does not match a physical file or directory:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [L]

How to re-write this URL in PHP using .htaccess?

I am working on one website.
On this website, porfile URL currently is like this:
http://eprofile.co/eprofile.php?user=degroundshaker
I want to rewrite this URL as:
http://eprofile.co/degroundshaker
This, is addon domain so its files are under a folder called "eprofile.co" in my cPanel and there is one .htaccess file.
So, i need solution and please let me know what rule i need to add and what should be the complete format in .htaccess
I m newbie in .htaccess.
Try this:
RewriteEngine On
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]
Then you need to change all of your profile links from looking like this:
http://eprofile.co/eprofile.php?user=degroundshaker
to looking like this:
http://eprofile.co/degroundshaker
EDIT:
Alternatively, you could do:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/eprofile.php
RewriteRule ^([^/]*)$ /eprofile.php?user=$1 [L]

htaccess mod_rewrite exclude images/css

I have created a simple mod rewrite for my site:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(cgi-bin|css|images|js|gfx)/?.*$ [NC]
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
but it doesn't seem to be working (as in all the images/js etc don't show up)
e.g. http://mydomain.com/filter/model/Sony+Ericsson+Xperia+Arc+S+White/mins/+3000/texts/+-1/freegifts/FREE+Nintendo+3DS+Black/network/Orange/merchant/
original url: http://mydomain.com/fiverrr.php?model=Sony+Ericsson+Xperia+Arc+S+White&mins=+3000&texts=+-1&freegifts=FREE+Nintendo+3DS+Black&network=Orange&merchant=Contract
Where am I going wrong, and is this the best method?
Thanks so much
Try to put there another condition - htaccess will rewrite it only if requested file does not exist:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^filter/model/([^/]*)/mins/([^/]*)/texts/([^/]*)/freegifts/([^/]*)/network/([^/]*)/merchant/([^/]*)$ /fiverrr.php?model=$1&mins=$2&texts=$3&freegifts=$4&network=$5&merchant=$6 [L]
Also I recomend you to check full paths. Check if you have images in HTML code like: http://mydomain.com/images/img.jpg - relative paths like ./../images/img.jpg will not work!

Resources