htaccess url rewriting script doesn't work - regex

In my website,when it is clicked "www.anketurkiye.com/ekonomianket", I want to my site to redirect to www.anketurkiye.com/ekonomibuton.php page. I have written the following script.However it doesn't run correctly. How can I solve this problem.
RewriteEngine on
RewriteRule ^/ekonomianket$ ekonomibuton.php [NC,L]

Replace that RewriteRule line with this:
RewriteRule ^ekonomianket/?$ ekonomibuton.php [NC,L]
In your .htaccess leading slash is stripped off so you need to match without leading slash.

Related

htaccess - rewrite URL ending with specific string and capturing the unique part

I want to feed several URLs into a single php file that will handle the contents of the page, the URLs are like
domain.com/fashion-registration
domain.com/singing-registration
I want to capture URLs ending with -registration and feed fashion or singing into the page but it doesn't seem to be working. This is what I tried
RewriteRule ^(.*)$-registration category.php?link=$1 [NC,L,QSA]
Could you please try following.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(.*)-registration/?$ [NC]
RewriteRule ^(.*)$ /category.php?link=%1 [NE,NC,L]
OR you could try following too, one without RewriteCond.
RewriteEngine ON
RewriteRule ^(.*)-registration/?$ /category.php?link=$1 [NE,NC,L]
Problem in OP's attempt: Since you have used ^(.*)$ and after that you are using -registration in your regex that's why your regex is NEVER going to match -registration

Apache url rewriting and loop

setting url rewriting to have nice urls, i have existing urls like that :
/xxx/test.php
but in the background, it is allways going to the same script with a query :
/xxx/index.php?id=test
with the following rewrite :
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
it's working fine.
now, there are old urls still like /xxx/index.php?id=$1
and i want to get rid of these old urls, meaning I want all of them to be for the users like /xxx/test.php with a 301 redirect
i did a rewrite for this but then i'm entering a loop despite the L flag
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^xxx/index\.php$ /xxx/%1.php? [R=301,L]
? is it possible to handle that and how ?
and other to describe it is allways use the script :
/xxx/index.php?id=$1
but allways have the right url in the browser displayed
Keep your existing
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
which appears to work fine.
Add in these two lines before that which will catch if there is an id= and strip it out of the URL.
RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L,R=301]
^ start of query string
([^&])* any character except &
(.*) any following characters
So if query string is id=test&something=else RewriteRule will append exactly that and nothing else as there is no more QSA flag.
Try those 3 lines together (htaccess test website), here is the full htaccess file:
RewriteCond %{QUERY_STRING} ^id=([^&]*)(.*)$
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=%1%2 [L]
RewriteRule ^xxx/([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]
Make your RewriteRule not match index.php or remove the QSA flag.
Say you type test.php well now you will go to index.php?id=test
Then Rewrite occurs again and you will go to index.php?id=index&id=test
Then it will occur again because the page is different: index.php?id=index&id=index&id=test etc.
So add in your regex a negative lookahead: xxx/(?!index)([0-9a-z\-]*)\.php
Try:
RewriteRule ^xxx/(?!index)([0-9a-z\-]*)\.php$ /xxx/index\.php?id=$1 [QSA,L]

removing multiple groups of slashes everywhere in URL in .htaccess

I currently have a website where guests are able to access each url with any number of slashes to separate folder names. For example, if a URL is supposed to be:
http://example.com/one/two/three/four
Then users could access the same page via any of the following:
http://example.com/one//two///three////four/////
http://example.com/one/two////three/four/////
http://example.com///one///////////two////three/four/
http://example.com///////////one///////////two/three/four
However, I want the above example urls to only redirect users to this URL:
http://example.com/one/two/three/four
This is my .htaccess file to attempt to stop the enormous slashes:
RewriteCond %{ENV:REDIRECT_STATUS} !^$
RewriteRule .* - [L]
RewriteRule ^(.*)/+$ /$1 [R=301,L,NC]
RewriteCond %{REQUEST_URI} ^/+(.*)/+$
RewriteRule .* /%1 [R=301,L]
The third line successfully stops trailing slashes on long URLs. The 4th and 5th lines are my attempt to stop trailing slashes right after the domain name, but that was unsuccessful.
The reason why I ask this question is because I don't want google to catch me for duplicate content and with adsense active on the site, google will likely scan all the URLs that I access.
Is there a RewriteCond/RewriteRule combo I can use to strip the middle slashes or is it more involved?
You can use this rule for removing multiple slashes anywhere in URL except query string:
RewriteCond %{THE_REQUEST} \s[^?]*//
RewriteRule ^.*$ /$0 [R=302,L,NE]
This works for me:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]

htaccess replace spaces just if URL starts with certain words

Im trying to replace all %20 with + signs but just when the url starts with /some/dir/. For example:
http://mydomain.com/some/dir/replace%20here
should be redirected to
http://mydomain.com/some/dir/replace+here
but
http://mydomain.com/test/dir/replace%20here
should not replace the %20
So far I have:
RewriteCond %{REQUEST_URI} ^(/some/dir/)
RewriteRule ^([^\s%20]+)(?:\s|%20)+([^\s%20]+)((?:\s|%20)+.*)$ $1$2+$3 [N,DPI]
RewriteRule ^([^\s%20]+)(?:\s|%20)+(.*)$ /$1+$2 [L,R=301,DPI]
but that stills replaces the %20 in /test/dir
What am I missing?
Thanks!
Your rules are not correct and moreover DPI flag is not supported in all Apache versions. Change your Rewrite rules to this:
RewriteRule ^(some/dir)/([^\s%20]+)(?:\s|%20)+([^\s%20]+)(.*?)/?$ $1/$2+$3$4 [L,NC,R=301]
RewriteRule ^(some/dir)/([^\s%20]+)(?:\s|%20)+ $1/$2 [L,NC,R=301]

Mod-Rewrite for dynamic URLs

I'm trying to rewrite my URLs to be more SEO friendly, and I'm doing this in my .htaccess file using the following code:
RewriteEngine On
RewriteRule blog/(.*)/$ blog/index.php?&link=$1 [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
That code allows me to rewrite http://example.com/blog/index.php?link=22 into http://example.com/blog/page-title/
The only problem here, is that I'm trying to get it to work without the ending slash (/). I've tried removing it in the third line of the .htaccess code (right fater the $2), but that just breaks everything and the rewrite doesn't work at all.
Any help is greatly appreciated.
Try now, both with or without slash should work.
RewriteEngine On
RewriteRule blog/(.*)/?$ blog/index.php?&link=$1 [NC]
RewriteRule ^/*(.+/)?([^.]*[^/])$ http://%{HTTP_HOST}/$1$2 [L,R=301]