htaccess to check if query string exist otherwise use previous rule - apache

I am trying to convert this url
http://127.0.0.1/kurumsal/index.php?sayfa=referans&page=2
to this
http://127.0.0.1/kurumsal/index/sayfa/referans/page/2/
but not every page has pagination which refers to page=2 query string. i want to just use first part of query string if 'page' query string do not exist.
So it would be http://127.0.0.1/kurumsal/index/sayfa/referans/ without pagination query string.
followed some tutorials but no luck, here is my htaccess
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)page=($|&)
RewriteRule ^sayfa/([^/]*)/([^/]*)/$ /kurumsal/index.php?sayfa=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sayfa/([^/]*)/$ /kurumsal/index.php?sayfa=$1 [L]

Try using this rule:
RewriteEngine On
RewriteRule ^kurumsal/index/sayfa/([^/]*)/page/([^/]*)$ /kurumsal/index.php?sayfa=$1&page=$2 [L]

You can use this rule in root/.htaccess :
RewriteEngine on
RewriteCond %{THE_REQUEST} /kurumsal/index\.php\?sayfa=([^\&]+)(?:&page=)?(.*?)\sHTTP [NC]
RewriteRule ^ /kurumsal/index/sayfa/%1/%2? [L,R]
RewriteRule ^(?:kurumsal/)?index/sayfa/([^/]+)/?(.*?)/?$ /kurumsal/index.php?sayfa=$1&page=$2 [L]

Try this
RewriteEngine On
RewriteBase /kurumsal/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index/sayfa/([^/]*)/page/([^/]*)/$ /kurumsal/index.php?sayfa=$1&page=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^index/sayfa/([^/]*)/$ /kurumsal/index.php?sayfa=$1 [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]

Rewrite urls in htaccess file - remove query string

I have never edited the .htaccess file before so sorry for my awful attempt
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://example.site/index/%1 [R=302,L]
I am tryting to rewrite http://ex.com/shop/?s=sa to look like http://ex.com/shop/sa
I am assuming you are using rewriterules in shop directory and index.php is handling the data then try with below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ index.php?s=$1 [L]

Rewrite GET Query String using .htaccess

I want to Rewrite my GET Query String from this -
http://www.example.com/verify.php?key=547b52f2b5d20d258e62de1e27b55c
to this
http://www.example.com/verify/547b52f2b5d20d258e62de1e27b55c
I am using the following rule but it does not seem to work -
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
RewriteRule ^[A-Za-z-]+/([A-Za-z0-9-]+)/?$ verify.php?key=$1 [NC,L]
Use the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /verify\.php\?key=([^\s&]+) [NC]
RewriteRule ^verify\.php$ /verify/%1? [R=301,L]
RewriteRule ^verify/([^/]+)/?$ /verify.php?key=$1 [NC,L]

How to redirect invalid URL in .htaccess?

/(directory/?param=value) cannot change this url
directory/?param=value
to
directory/value
It's not work:
RewriteCond %{REQUEST_URI} ^/directory/.*
RewriteRule ^/directory/\?param=(.*) /directory/$1 [L]
My problem is how to replace ?param= ???
These rules should work for you:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(directory)/\?param=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L]
RewriteRule ^(directory)/([^/]+)/?$ /$1/?param=$2 [L,QSA,NC]
You can't match against the query string (everything after the ?) in a RewriteRule, try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?directory/(.+)$ /directory/?param=$1 [L]
That takes: directory/value and rewrites it internally to serve the content at directory/?param=value.

htaccess rewrite

I am working on a project and it utilizes a shortened url for redirection, I've gotten the following code to work perfectly:
XBitHack Off
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} \/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./getdeck.php?deck=%1 [L]
Now I want to add a second redirect rule for /a/([0-9A-Za-z]{4} but no matter how I write it, the new rule fails. Can anyone help explain what I'm missing?
Examples I have tried:
RewriteCond %{REQUEST_URI} ^/a/.*$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
or
RewriteCond %{REQUEST_URI} ^/a/([0-9A-Za-z]{4})$ [NC]
RewriteRule ^(.*) ./deck/makedeck.php?deck=%1 [L]
You might have to repeat the first two conditions.