how to create .htaccess long title url - .htaccess

I am creating PHP blog viewing script I have added htaccess file to remove URL char this is my working url http://localhost/zblog/source/4/title but not working long title like this URL http://localhost/zblog/source/8/xcccxcx-cxcx-ccff single title working only
RewriteEngine on
RewriteRule ^source/([0-9]+)/([0-9a-zA-Z]+)/?$ source.php?srcid=$1&title=$2 [NC,QSA,L]

Add the -char in your rule.
With \ (or without in the end), because otherwise it's an interval.
RewriteEngine on
RewriteRule ^source/([0-9]+)/([0-9A-Z\-]+)/?$ source.php?srcid=$1&title=$2 [NC,QSA,L]

Related

.htaccess: How do I redirect to a PHP script only when accessing a specific subdirectory?

I created a .htaccess file that redirects .png QR code requests to a PHP QR code generator.
RewriteEngine On
RewriteBase /qr/
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.png
RewriteRule ^(.*)$ access-code.php?id=%1 [QSA,L]
It works well for the original intent, but also sends EVERY .png request to the same script. I need to alter the RewriteCond to only accept .png files if in the /qr/access-code/* directory.
I placed the .htaccess file in the following directory:
/qr/.htaccess
Example request that works well as intended (not an actual link):
http://mywebsite.com/qr/access-code/12345678.png
Example request that "works", but I don't want it to get redirected:
http://mywebsite.com/qr/img/background.png (not an actual link)
How can I adjust my .htaccess condition to make my rule function as intended?
You can use the following :
RewriteEngine On
RewriteBase /qr/
##If /document_root/dir/foo.png is an existent file##
RewriteCond %{DOCUMENT_ROOT}/access-code/$1 -f
##Rewrite /foo.png to access-code.php?id=foo.png
RewriteRule ^(.+\.png)$ access-code.php?id=$1 [QSA,L]

htaccess rule to consider double slashes as single

I am working on a project built using codeigniter. And the mistake I have made is that I have added "/" after the base_url() method in many links(href) I have given on the project files as shown below.
<? echo base_url();?>/about
So the URLS are looking like
http://myproject.com//about
in place of
http://myproject.com/about
And because of that, it is resulting to 404 error.
Now the situation is that the site is ready but I am only allowed to update the .htaccess file of the project to fix this.
So is there any rule I can write in .htaccess file so that it considers the double slashes in the URL as single slash and open the specific page?
Funny thing is, it was already considering double slashes as single slash somehow and opening the "About" page on the development server. But now as the server is changed, I have started facing the 404 issues.
You can use this rule as very first rule in your DOCUMENT_ROOT/.htaccess file to remove multiple //:
RewriteEngine On
RewriteCond %{THE_REQUEST} //
RewriteRule ^(.+?)/?$ /$1 [R=302,L,NE]
actually you have set the base url as
$config['base_url'] = 'http://myproject.com/';
and when u are using it like this
<? echo base_url();?>/about
it is adding to // so you have to change your page like this
<? echo base_url();?>about
and if you cant change your path then you can set your htaccess as
RewriteEngine On
RewriteCond %{THE_REQUEST} //
RewriteRule ^(.+?)/?$ /$1 [R=302,L,NE]

modrewrite website with slash in .htaccess

How can I translate an URL like:
http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
to:
http://localhost/mySite/OFFERS/ARTICLE/DETAIL
if one parameter is empty it should still work like this:
localhost/mySite/?link=OFFERS&sublink=ARTICLE
localhost/mySite/OFFERS/ARTICLE
Extra problem: There is an enter page under index.php and the rewrite should work with index2.php. Best would be if it would work under localhost and on live system without changes.
Currently I'm using: RewriteRule ^([^/.]+)$ index2.php?link=$1 [L]
But that only works for one parameter and I couldn't improve this code for ages ^^
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]
Note that localhost/mySite/OFFERS/ARTICLE links to localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= and not localhost/mySite/?link=OFFERS&sublink=ARTICLE.
Should not be a big issue, but make sure the PHP code doesn't us isset($_GET['subsublink']).
Try adding the following to your htaccess file in the mysitedirectory of your site.
RewriteEngine on
RewriteBase /mysite/
# rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
# http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
#assumes that index2.php is in the root directory of site
RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]
#redirect index to index2
#if you do not want to redirect, just server rewrite, remove the R=301 below
RewriteRule ^index\.php$ index2.php [NC,L,R=301]

.htaccess redirect non existing pages based on index.php page with wildcard and something after it to index.php

Please help, some website is arbitrarily adding dynamic query strings to my home page.
For example my home page is www.mysite.com/index.php and they link to many links like this:
www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
And those links are opening on my site, content of page are the same for every page, just like my original www.mysite.com/index.php
There are few hundreds of that links pointing to my site. So how I can redirect this:
www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
...
to
www.mysite.com/index.php or just to www.mysite.com/
This what I tried so far in my .htaccess file
RedirectMatch 302 ^index.php?a= http://www.www.example-ste.com/
RewriteRule ^/index.php?a=(.*) http://www.example-ste.com/
But still pages are opening on site.
Another similar question.
How to redirect pages ending with "?pagewanted=all" to the same page but with out that "?pagewanted=all"
For example I need to redirect page:
www.mysite.com/something-something/something.html?pagewanted=all
to
www.mysite.com/something-something/something.html
Hello.
I just noticed something. I needed URL redirection rule which will redirect pages like:
www.mysite.com/index.php?a=something-something-something
www.mysite.com/index.php?a=something-other
www.mysite.com/index.php?a=some-other-thing
to home page of site, root. And you gave me this code:
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .* http://www.mysite.com/? [R=301,L]
And I must say it works fine, it does that, but I just noticed that it somehow blocks or redirect all links containing ?a= for example on some temporary pages I have links like:
i.php?a=something-something-something
So, can you adopt code just for pages based on index.php like:
www.mysite.com/index.php?a=something-something-something
and not for links with:
i.php?a=something-something-something
If I am right it works on all links with "a=" but I need just for "index.php?a="
Try adding the folloginw to the top of your .htaccess file
RewriteEngine On
RewriteBase /
#if the query string has an a parameter
RewriteCond %{QUERY_STRING} (^|&)a= [NC]
#Redirect and remove query string parameters
RewriteRule .* http://www.mysite.com/? [R=301,L]
#if the query string has a pagewanted parameter
RewriteCond %{QUERY_STRING} (^|&)pagewanted=all [NC]
#Redirect and remove query string parameters for html pages
RewriteRule (.+\.html) http://www.mysite.com/$1? [R=301,L]
Edit.
Added rule to remove pagewanted=all

Redirect hundreds of urls with htaccess

hello my url structure right now for the majority of my links is:
www.url.com/category1/sample-keyword.html
I am looking to redirect them to the new url that has dropped the word sample from the url structure ie to this:
www.url.com/category1/keyword.html
what should i put in htaccess that auto redirects all the urls in the www.url.com/category1/ section to redirect to the new url structure?
This should do the trick:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*/)sample-(.*)$ $1$2 [L,R=301]
It will match all URLS with the substring /sample- and strip it from the URL. Depending on your site organization, you may need to adjust the pattern, but that should be a good jumping-off point.
RewriteEngine On
# Redirect sample-*.html to *.html
RewriteRule ^\/?category([0-9]+)\/sample\-([^\/]+)\.html$ http://www.url.com/category$1/$2.html [R=301]
# Serve *.html
RewriteRule ^\/?category([0-9]+)\/([^\/]+)\.html$ page.php?category_id=$1&keyword=$2 [L]

Resources