What is the code Wordpress uses to make SEO friendly URLs? - regex

Can anyone point me to the standard code Wordpress uses to turn a blog post url from this:
http://myblog.com/?p=123
into this:
http://myblog.com/this-is-my-articles-title-i-think
If the post title was: This Is My Article's "Title" (I Think)

WordPress doesn't redirect or rewrite requests. index.php serves the content based on the original request URI. It catches requests that don't exist and figures them out from the plug. This code is in the WordPress document root in .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
If you want to know how WP generates the permalink slugs, look in wp-includes/formatting.php for this function:
function sanitize_title_with_dashes($title) { ... }

In your wordpress admin go to settings > permalinks and select 'post name' then save changes and you're all set.

Related

Why URL parameter is not working rewrite URL htaccess?

I am trying to get blog details page with rewriting URL
like this ->>
www.sitename.com/blog/test-post
My code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([a-z]+) details.php [L]
</IfModule>
My problem is , it is coming the details page, but the blog list page
www.sitename.com/blog/
also redirecting to blog details page. how can i avoid this. i also tried with number code like this
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/?([0-9]+) details.php [L]
</IfModule>
the url with number parameter works fine, but letters is not working. how to solve this?
You need to skip files and directories from your rule:
<IfModule mod_rewrite.c>
RewriteEngine on
# 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
RewriteRule . details.php [L]
</IfModule>

Regexp in .htaccess

I've got a simple .htaccess requirement that I'm failing on and would appreciate a pointer.
Content from an old CMS is being migrated to WordPress. The old CMS posts are formatted in the following way:
{postid}-example-post-title
eg
101-this-is-an-old-post
I have a mapping table that links the old post ids to the new WordPress post ids, so in theory \{oldid-*} should map nicely to the WordPress standard \?p={newid} format. I expected the following to work to redirect old url 101-this-is-the-post to the WordPress post 1660:
Redirect 301 /101-.*$ /?p=1660
The regexp /101-.*$ matches correctly, but for some reason my .htaccess file doesn't seem to recognise the regular expression. Here's my full htaccess file:
RewriteEngine Off
SetEnv DEFAULT_PHP_VERSION 56
DirectoryIndex index.cgi index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Redirect 301 /101-(.*) /?p=1660
I've tried moving the Redirect to the top of the file, the bottom of the file and between the IfModule block to no avail. I'm clearly missing something - any thoughts appreciated
As per julp's comment above, the solution to this problem was to use RewriteRule. The following worked correctly, placed withing the IfModule block :
RewriteRule 301 /101-(.*) /?p=166

htaccess redirect /foldername/page_name to /page-name

I have an issue:
Recently I replaced my old website (no CMS) with a new website (and WordPress as CMS). But a lot of people still try to access the website by old URL's.
Obviously I want to redirect them to the right page.
For example: the previous URL was: http://domain.com/foldername/page_name
On the new website that same page is located at http://domain.com/page-name
Note #1: The underscore has been replaced with a dash
Note #2: Some pages may include more underscores. For example: http://domain.com/foldername/an_other_page_name
Note #3: Since I use WordPress as CMS, there is already some code in my .htaccess file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Since I'm not as good with htaccess as some of you I hope someone has the solution, since I'm losing visitors because they can't find the right page.
Just below RewriteBase line you can use this recursive rule:
RewriteRule "^(foldername)/([^_]*)_+([^_]*_.*)$" /$1/$2-$3 [N]
RewriteRule "^(foldername)/([^_]*)_([^_]*)$" /$2-$3 [L,R=301]

301 redirect to new domain and redirect individual old pages at the same time

I hope you can help me with this 301 issue.
Introduction
I've just published a re-design of a website with new clean urls. The old webpage had urls like this: www.domain.dk/Default.aspx?ID=66. And the new website urls look like this: www.domain.com/contact
So I wan't to redirect all these old urls to the new ones, and therefor i'm not keeping the old urls and no general rule can be applied.
That's just simple 301 redirects, but at the same time the old domain points to a new domain, and this is where things get dirty, I think. The old domain was www.domain.dk, but i wan't to 301 all traffic to the new domain www.domain.com and at the same time I wan't to make all the individual 301 redirects.
The problem
When I click on the link www.domain.dk/Default.aspx?ID=66 in Google I get this URL in my browser: www.domain.comindex.php/?ID=66.
On other links I get www.domain.comdefault.aspx/?ID=2
So the redirecting to the new domain works fine? But the individual redirects doesn't apply at all.
The code
This code is pasted as is from my .htaccess file on the server running apache.
The first bit is auto-generated by Concrete5 CMS to make pretty URLs.
# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --
This is the code I found to 301 redirect all traffic to urls that is not using www.esvagt.com to www.esvagt.com
## --- 301 Redirects --- ##
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
This is just one of the manual 301 redirects.
## General - Redirects ##
redirect 301 /Default.aspx?ID=66 http://www.domain.com/contact/contact-us
Thanks in advance. If you need more information I'll gladly provide that.
I have zero knowledge about writing code in .htaccess, so I'm pretty clueless. I hope you can help. :)
Avoid mixing mod_rewrite and mod_alias rules.
Ordering of rules is also very important so have 301 rules first and then have your catch all controller rule
Use this code for 301 redirect:
## --- 301 Redirects --- ##
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.dk$ [NC]
RewriteRule ^ http://www.domain.com%{REQUEST_URI} [R=301,L]
## General - Redirects ##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+Default\.aspx\?ID=66[&\s] [NC]
RewriteRule ^ http://www.domain.com/contact/contact-us? [R=301,L]
# -- concrete5 urls start --
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule . index.php [L]
</IfModule>
# -- concrete5 urls end --
RewriteRule www.domain.dk/Default.aspx?ID=66 domain.com/contact/contact-us [R=301,L]
If you want to redirect all posts automatically:
RewriteRule /Artical.aspx?ID=(.+?) domain.com/article-title-$1.html [R=301,L]
or
RewriteRule /Artical.aspx?ID=(.+?) domain.com/Post.aspx?ID=$1 [R=301,L]
Then the dynamic url Artical.aspx?ID=20 will be redirect to http://www.domain.com/article-title-30.html,
I successfully apply this method for my blog http://downloadapp.info

.htaccess issue with ExpressionEngine and YOURLS

I have a primary site running ExpressionEngine and I am trying to get YOURLS running in a subfolder called "work", ie: http://foo.com/work/ for short urls and EE is running at the root, ie: http://foo.com/. Please note there is no 'www'.
The problem I am having is that when I use a short URL and a user adds "www" to the URI, such as http://www.foo.com/work/123 I get a redirect chain that looks like this:
http://www.foo.com/work/123 302 redirects to
http://www.foo.com/work 301 redirects to
http://www.foo.com/work/ which returns 200
Everything works fine if you omit the 'www' from the URI. YOURLS is set up to use the non-www but the worry is that some users might habitually type the 'www' as these URIs are being placed on printed ads.
The root .htaccess file looks like this:
SetOutputFilter DEFLATE
DirectoryIndex index.php index.html
# BEGIN ExpressionEngine Rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.foo.com [NC]
RewriteRule ^(.*)$ http://foo.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
# END ExpressionEngine Rewrite
The /work/ (YOURLS) .htaccess looks like this:
# BEGIN YOURLS
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /work/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /work/yourls-loader.php [L]
</IfModule>
# END YOURLS
How can I get the 'www' URIs to work like the non-www URIs do without completely hosing EE? :)
I faced a similar issue. Some end users or people publishing the short urls tend to append them with www while the short url was bought with the intention of using it without www.
I also installed the YOURLS redirect index plugin to forward homepage visitors to our regular homepage.
So I had a few different types of urls:
short domain homepage, should lead to the regular https homepage
short domain YOURLS admin panel, should lead to the https admin panel
short domain shortened urls, should lead to the long url
I wanted all urls to work with or without www and make sure http was forwarded to https.
My solution was to start .htaccess with these lines:
# Force HTTPS (because of passwords via /admin) and use clean domain (non www)
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www.examp.le [NC]
RewriteRule ^(.*)$ https://examp.le/$1 [L,R=301]