How to insert part of url via htaccess - regex

I've got old URLs like this:
http://example.com/news/newscategory/77654
And it should be rewritten to this URL structure:
http://example.com/news/newscategory/id/77654
So the news is static here as it's the folder. the newscategory can be something else (multiple categories). And I want to redirect the user (301) to new url with id/ part.
I fetch the url via php where the news is key, newscategory is value, id is key and number at the end is again, value.
I'm using this htaccess for this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
</IfModule>
Then I use some php library to fetch the values.
Edit: What I'm asking is that I want to redirect user from first url to second one so that I can fetch the id.
The first link is already working, but I still have links to the old url structure that leads to category view. Live urls:
old structure: http://www.autonet.ee/uudised/paevauudised/77665
new structure: http://www.autonet.ee/uudised/paevauudised/id/77665
the old one should redirect user to the new link.

You can use:
RewriteEngine On
# add /id/ in /news/... URLS
RewriteRule ^(news/[^\w-]+)/(\d+)$ /$1/id/$2 [R=301,L,NC]
# front controller rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Try this rule if i understand correctly
RewriteRule ^([.*])/([.*])/([.*])/([.*])$ index.php?$1=$2&$3=$4
please explain if missed something.
EDIT
okay, according to you news and id are static but their value is dynamic please try the rule below
RewriteRule ^(news)/([.*])/(id)/([.*])$ index.php?news=$1&id=$2

Related

.htaccess redirect rules after forum software migration

We're currently upgrading our site forum software, and need to redirect all old forum thread URLs to the new format.
We've tried a couple of things but can't get this to work, as different .htaccess redirect rules are conflicting.
Old format:
https://www.example.com/beta/news-and-announcements/1354-thread-name.html
New format:
https://www.example.com/beta/threads/thread-name.1354/
We would like these to 301 redirect to the new URL structure to ensure everything keeps working.
news-and-announcements in the first URL is a dynamic sub-forum name.
1354 is the thread ID - this is really the only bit that needs to be kept and moved over to the new URL.
thread-name - forum thread name - as long as the ID is in the correct place this will get rewritten to be correct by the new forum software.
This is the default .htaccess mod_rewrite section:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
We've tried to expand this to cover some additional redirects from the old URL structure like this, but it's not working:
RewriteBase /beta/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.*)\/(\d*)-(.*).html$ threads/$2 [NC,L]
RewriteRule ^(data/|js/|styles/|install/|favicon\.ico|crossdomain\.xml|robots\.txt) - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
The above redirect doesn't work, and also breaks the AdminCP URLs somehow:
https://www.example.com/beta/admin.php
Any help on getting this working would be hugely appreciated!
test it!
RewriteRule ^news-and-announcements/(\d+)-(.+).html /threads/$2.$1/? [R=301,NC,L]
add this codes below RewriteEngine On on .htaccess file

my url is xyz.com/songs.pgp?lang=3&movie=198, I want to show xyz.com/moviename.html

my url is xyz.com/songs.php?lan=3&movie=198,
I want to show xyz.com/moviename.html, is it possible with .htacces..? please help me
In my .htaccess file am using -
RewriteRule ^([^/.]+)/([^/.]+)$ songs.php?lan=$1&movie=$2 [QSA,L]
Its showing - xyz.com/telugu/moviename but i want to show xyz.com/moviename.html
please help me am totally new for this
In this cases you can do like the job which CMS and Frameworks do:
First redirect all requests to index.php in the .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw|xml|jpg|ajx))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
After this, all request will redirect to your index.php. for example DOMAIN/a.html and DOMAIN/a/b.html and ...
At this time $_SERVER['REQUEST_URI'] is the address which you have requested for example /a.html and you can decide which data should show according to the current URL.
If you want
xyz.com/moviename.html
then you will need to write a rule for every movie. i.e.
RewriteRule ^moviename.html songs.php?lan=3&movie-123
RewriteRule ^another-moviename.html songs.php?lan=4&movie-124
A simpler way would be to include the movie_id somewhere in the URL:
# Catch xyz.com/123/the-movie-title
RewriteRule ^([0-9]+)/([^/.]+).html songs.php?lan=3&movie=$2
this way would capture all movies without the need to a specific rule for each. You can then in your code check if the ID is valid or not. You can also grab the movie title and check it matches the URL and if not do a redirect to the correct one based off the movie id.
Given your comments, I think you should try a different method.
Update your .htaccess to route all request through a single file (index.php).
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?route=$1 [L,QSA]
Then in index.php you can do something like (please note this need to be implemented in a secure fashion, the below is a rough outline):
<?php
// Copy route into variable.
$route = $_GET['route'];
$slugs = array_filter(explode('/', $route));
// Check there is a slug. e.g. a-great-movie
if(count($slugs) > 0) {
// Perform a SQL/Database query here so see if $slug[0] matches a movie.
// Oviously do this with you database of chose and sanitise input.
// e.g. SELECT id FROM movies WHERE slug = $slug[0]
// Check a match was found if so load your page content.
if($result->rowCount() > 0 ) {
// Show page content.
}
else {
// Show a 404 page?
}
}
else {
// Show homepage.
}
After doing the above you can now go to urls like:
xyz.com/a-great-movie
xyz.com/another-great-movie
Of course you can do all thing with a routing library of your choice as well.

301 Redirect All Blog Posts To New Subfolder

I am currently using this .htaccess rewrite to get pretty urls for my blog posts:
RewriteRule ^([a-zA-Z0-9-]+)$ /item.php?slug=$1 [L]
This ends up looking like:
example.com/my-new-awesome-blog-post
What I would like is to move all my posts to a new folder called items so that it would look like:
example.com/items/my-new-awesome-blog-post
I not only want to move all blog posts to the subfolder, but also to ensure it is a proper 301 redirect so there is no issue with SEO.
Thank you for your help/advice.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -MultiViews
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+items/ [NC]
RewriteRule ^ items%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^items/([a-zA-Z0-9-]+)/?$ items/item.php?slug=$1 [L,QSA]

Error getting .htaccess to direct googlebot using _escaped_fragment_

I am trying to get my pages indexed on google using a prerendering service for my backbone app.
I know the setup works fine when I specifically add googlebot to the useragent list but Ive been advised against this in favor of using the _escaped_fragment_ method. Only problem is the _escaped_fragment_ parameter isn't getting passed correctly. Can some help please?
thanks!!!
# html5 pushstate (history) support:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTPS} !on
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
# If requested resource exists as a file or directory
# (REQUEST_FILENAME is only relative in virtualhost context, so not usable)
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} -d
# Go to it as is
RewriteRule ^ - [L]
# If non existent
# If path ends with / and is not just a single /, redirect to without the trailing /
RewriteCond %{REQUEST_URI} ^.*/$
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)/$ $1 [R,QSA,L]
# Handle Prerender.io
RequestHeader set X-Prerender-Token "xxxxxxxx"
RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
# Proxy the request
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) http://service.prerender.io/https://www.example.com/$2 [P,L]
# If non existent
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index
RewriteRule (.*) index.html [L,QSA]
</ifModule>
All the apache modules are loaded and working.
So the .htaccess is actually correct... here Google's official answer.
Quote from http://productforums.google.com/forum/#!category-topic/webmasters/crawling-indexing--ranking/bZgWCJTnl08%5B1-25%5D by John Mueller (google employee)
Looking at your blog's homepage, one thing to keep in mind is that the Fetch
as Googlebot feature does not parse the content that it fetches. So when you
submit toddmoyer.net/blog/ , it fetches that URL. After fetching the URL, it
doesn't parse it to check for the "fragment" meta tag, it just returns it to
you. However, if you fetch toddmoyer.net/blog/#! , then it should rewrite the
URL and fetch the URL toddmoyer.net/blog/?_escaped_fragment_= .
When we crawl and index your pages, we'll notice the meta-tag and act
accordingly. It's just the Fetch as Googlebot feature that doesn't check for
meta-tags, and instead just returns the raw content.

Rewrite rule with 2 variables with "/" separation

I have an .htaccess file located in a folder "/mixtapes/" I am trying to get the URL mydomain.com/music/downloads/mixtapes/this-title/id to execute mydomain.com/music/downloads/mixtapes/item.php?title=variable1&id=variable2
I currently have the below way somewhat working but it only uses the id and I need both variables (../mixtapes/title/id)separated by "/" and for some reason with the below code the index page inside "/mixtapes/" does not work.I am stumped! I am somewhat new to this and any help is greatly appreciated!
BTW on my index page the passing url to item.php page is rewritten to <a href="title/id">I just cant seem to get it to properly execute item.php?title=a&id=b with the format mixtapes/title/id
Current htaccess file located in "/mixtapes/"
# turn mod_rewrite engine on
RewriteEngine On
# rewrite all physical existing file or folder
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} !-d
# allow things that are certainly necessary
RewriteCond %{REQUEST_URI} "/css/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/images/" [OR]
RewriteCond %{REQUEST_URI} "/javascript/"
# rewrite rules
RewriteRule ^mixtapes/item.php(.*) - [L]
RewriteRule ^(.*) item.php?id=$1 [QSA]
The comments in your .htaccess actually state wrong
# turn mod_rewrite engine on
RewriteEngine On
# if requested URL is NOT a existing file
RewriteCond %{REQUEST_FILENAME} !-f [OR]
# or if requested URL is NOT a directory
RewriteCond %{REQUEST_FILENAME} !-d
# CSS, images, JavaScript are files and we will never pass this point when those are requested, next rules can be skipped
# rewrite rules
# rewrite /mixtapes/title/id to item.php?title=title&id=id
Rewrite ^mixtapes/([^/]+)/([0-9]+) item.php?title=$1&id=$2 [L]
# catch all other requests and handle them ( optional, default would be 404 if not a physically existing file )
Rewrite (.*) index.php [L,QSA]
I've assumed that your id is a numeric value.
Be aware with the use of the title in php. Don't output this directly but you can use it to verify your URL and redirect wrong title/id combos