I'm on my begining of learning PHP and very first steps of .htaccess, most of my new web is about main category and few subcategories.
Here are links examples i had before working out .htaccess RewriteEngine:
example.com/index.php?cat=email
example.com/index.php?cat=about&show=some
with help of .htaccess RewriteEngine i've convered them to:
example.com/email/
example.com/about/some/
Here is part of .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/$ index.php?cat=$1&show=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php?cat=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?cat=$1&show=$2 [L]
Now problem is that most of content have inside links like: "example.com/index.php?cat=about&show=some" Changing them all is option, but anyway... is there anything else could be done? I heard of some .htaccess option that autoconverts links to format you need without changing them manualy, so all links in PHP pages will be the same, but once user gets page loaded, links will be like (example.com/about/some/) Is there anything like that, or is there any other option to leave original link without changing them all?
Cheers!
Links on your site are created with your PHP scripts, Apache with htaccess can't magically change all this links in a raw.
Now what you can do, is redirect old URL to the new ones using htaccess, with a 301 redirection.
For example, if someone click on example.com/index.php?cat=email, Apache will redirect (= the URL will be changed and there will be another HTTP request) to example.com/email/, and then rewrite this to index.php?cat=email
Add this to your htaccess :
RewriteCond %{REQUEST_FILENAME} index.php$
RewriteCond %{QUERY_STRING} cat=([a-zA-Z0-9-]+)
RewriteRule ^ http://example.com/%1/? [L,R=301]
Anyway I strongly recommend you to change the links directly in your code, because even if the solution I've just explained should works (not tested), it's not really healthy to use redirection when you can avoid them.
Related
By using WordPress with Divi and custom php pages/scripts I ended with urls looking like this:
https://example.com/?day=today
https://example.com/?sport=1&league=123 (1=football and 123=premier_league)
I want to make them more user and SEO friendly and rewrite them in htaccess to:
https://example.com/?day=today ==> https://example.com/today
https://example.com/?sport=1&league=123 ==> https://example.com/football/premier_league
For the "day" link I tried this code, but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z\-\_]+)?$ /?day=$1 [L,QSA,NC]
</IfModule>
For the other example with sport and league I wanted to find a solution in which I will manually add old link and new link, tried redirect 301, but I think it can't work, right?
Redirect 301 https://example.com/?sport=1&league=123 https://example.com/football/premier_league
I also tried Redirection plugin in WP, but it created Rule which destroys page and makes error 500:
RewriteRule ?sport=1&liga=123 https://example.com/football/premier_league [R=301,L]
or
RewriteCond %{QUERY_STRING} ^sport=1&league=123$
RewriteRule ^$ https://example.com/football/premier_league [R=301,L]
which gaved address: https://example.com/premier_league/?sport=1&league=123
I was also wondering if I can make it with pure php, since in PHP I can easilly get proper strings for id's of the sports and leagues. Unfortunantelly changing "?sport=1&league=32758" into "?sport=football&league=premier_league" and then clean it with rewrite in htacces is not possible right now.
I'm not expecting whole code for both problems, I believe that some directions would be enough. I browse stack and google and coudn't find any examples with links that looks like mine, so example.com/?sport=1 not example.com/page?sport=1
After further investigation I believe the problem is in the way I'm opening my custom pages. I'm using shortcodes to initiate a script and inside the script my php code is included with parameters. I think that because of that I got to this page https://example.com/premier_league/?sport=1&league=123 which was pretty close, but not working.
Now my question is how to send those parameters to proper script but not into url.
I've been facing a problem with my WordPress app for over a month now and I couldn't make it work. I thought it was a problem with DNS server, then I thought I had a problem of caching, then a problem with the application (the wordpress redirecting it wrongly) and now I believe I have a problem with .htaccess
If you go to www.porta8080.com.br my website work fine, but when I remove the WWW it makes a redirect loop. Somehow it is not changing the URL, so it reloads the page and it's redirected again and again and again. I checked with cURL and same happens.
If I remove the .htaccess and change the permalinks settings to Query Strings it works. But anything that relies on URL rewriting (friendly URLs and stuff like that) fail.
Since I'm using OpenShift I think my conditions and rules are wrong. I'm using the default WP .htaccess and I added some things to force the WWW
This is my htaccess at the moment
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
First of all to enforce "www" in WordPress, there is simple, non-htaccess based solution.
i.e. Keep using standard htaccess file of WordPress and for www version. Go to WordPress Settings -> General
There you have two fields for your site's url: WordPress Address (URL) & Site Address (URL) , make sure both urls have http://www version in it.
Then WordPress will enforce www version for all Urls of your website.
This may solve your htaccess and redirect loop issue also?
I have some old SWF that has fixed links inside, so I need to rewrite all the links to redirect to new site I build on Wordpress.
I actually need to redirect all links that have FID=<123> to /me_<123> (that's the permalink I created for all pages I need to redirect)
For example:
http://kavor.org.il/KavOr07/Templates/showpage.asp?DBID=1&LNGID=2&TMID=843&FID=1997
Needs to go to http://kavor.org.il/me_1997
I tried to put this in .htaccess:
^FID=([^/.]+)/?$ /me_$1 [R]
but it has no effect and also for some reason slows down the entire site loading.
I know that Wordpress has its own rewrites so maybe it's interefering.
I would be glad for some help with that I got pretty lost in all the mod_rewrite rules...
To match query string you will need a RewriteCond with %{QUERY_STRING}
Try this rule:
RewriteCond %{QUERY_STRING} (^|&)FID=([^&]+) [NC]
RewriteRule ^KavOr07/?$ /me_%1? [L,R=302,NC]
I'd like to make shortened links for my site to be used in Tweets. I'm interested in a t.co-like URLs but confused on how to implement the redirect.
Here's how a link on my site typically looks:
https://mysite.com/item/this-is-a-book-on-toasters
Here's how I'd like the shortened link to look which would redirect to the above link
https://ms.co/Im8y2x
Based on googling how to do this, it looks I need to do a 301 redirect.
I'm using PHP, specifically Codeigniter and I guess there is 2 components: the PHP script and .htaccess.
Here's my .htaccess:
RewriteEngine on
RewriteRule ^/?$ https://mysite.com [L]
RewriteRule ^(.*)$ https://ms.co/$1 [R=301,NC]
The PHP I think I need is in here.
Unfortunately, I can't interpret the answers on this link to make a useful script. Might someone help with this? Also, does my .htaccess look right?
This should be the .htaccess code on your shortlinking website (ms.co):
RewriteEngine On
RewriteRule ^(.*)$ https://mysite.com/in.php?id=$1 [R=301,L]
The in.php should contain the script that decodes the $_GET['id'] (via the short hash decoding methods supplied in the link you supplied), matches it against an ID into your database, and retrieves page that it should redirect to.
By the way, the reason I didn't add a NC part in the code is because upper/lowercase (often) can yield different results when using decoding methods.
I've anaswered my own question. Here's what they .htaccess directive should look like given the question above:
RewriteCond %{HTTP_HOST} ^o-a\.co$ [NC] // the rewrite rule
RewriteCond %{HTTPS} =on //to enable HTTPS
RewriteRule ^(.*)$ https://mysite.com/page-to-handle-hash/$1 [L] //where in your application you want to send the 6-digit hash
I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs